private void ValidateName(ValidationContext context) { Debug.WriteLine("ConfigurationProperty.ValidateName called for " + this.Name); // CALLED! // TODO: Should verify IsDefaultCollection validition is enforced or check if parent element is a collection. NamingHelper.ValidationOptions options = NamingHelper.ValidationOptions.None; if (NamingHelper.RequiresValidation(this.Name, this, out options)) { Debug.WriteLine(" - ConfigurationProperty.ValidateName: Reqiures validation!"); string msg = ""; if (!NamingHelper.TryValidateAttributesItemNameProperty(this.Name, this.Name, options, out msg)) { context.LogError(msg, "RequiredProperty", this); } } }
protected override void OnValueChanged(ConfigurationProperty element, string oldValue, string newValue) { /* * // Accessing validator. * Microsoft.VisualStudio.Modeling.Shell.VsValidationController validator = new Microsoft.VisualStudio.Modeling.Shell.VsValidationController(element.Store); * if (!validator.Validate(element.Store, ValidationCategories.Menu)) * { * element.Store.TransactionManager.CurrentTransaction.Rollback(); * System.Windows.Forms.MessageBox.Show(validator.ValidationMessages.First().Description); * } */ //bool isParentCollection = this.FindAncestor<ConfigurationElementCollection>( // Don't run in an undo or when store is loading from file (CSD with issue could never open!). if (!element.Store.InUndoRedoOrRollback && !element.Store.InSerializationTransaction) { newValue = newValue.Trim(); // Trim and set new value in case user adds spaces by accident. element.Name = newValue; // Hard validation of the new name. //NamingHelper.ValidateRequiredName(newValue); // TODO: Should verify IsDefaultCollection validition is enforced or check if parent element is a collection. NamingHelper.ValidationOptions options = NamingHelper.ValidationOptions.None; if (NamingHelper.RequiresValidation(newValue, element, out options)) { string msg = ""; if (!NamingHelper.TryValidateAttributesItemNameProperty(newValue, newValue, options, out msg)) { throw new ArgumentException(msg, "RequiredProperty"); } } // When the name changes, set the XML name to the same name but camelCased. element.XmlName = NamingHelper.ToCamelCase(element.Name); } // Always call the base class method. base.OnValueChanged(element, oldValue, newValue); }
/// <summary> /// Determines whether property should be validated. /// </summary> /// <param name="val"></param> /// <param name="isParentCollection"></param> /// <param name="prop"></param> /// <param name="options"></param> /// <returns></returns> internal static bool RequiresValidation(string val, bool isParentCollection, ConfigurationProperty prop, out NamingHelper.ValidationOptions options) { // TODO: This is NOT always required (ie. IsDefaultCollection)! options = NamingHelper.ValidationOptions.IsXml; bool isActualDefaultCollection = (prop.IsDefaultCollection && isParentCollection); if (!isActualDefaultCollection) { options |= NamingHelper.ValidationOptions.IsRequired; } bool skipValidation = prop.IsDefaultCollection && string.IsNullOrEmpty(val); return(!skipValidation); }
// Checks whether name is a valid variable name. If empty, no check performed. /* * public static bool IsValidOrEmptyName(string name) * { * if (string.IsNullOrEmpty(name)) * { * return true; * } * else * { * return (IsValidName(name)); * } * } */ // Checks whether name is a valid xml name. // [20121217] Changing to simple SPACE check for now (most common validation issue). Was causing some issues and I do not have time to resolve yet. /* * public static bool IsValidXmlName(string name) * { * if (!string.IsNullOrEmpty(name) && name.IndexOf(' ') >= 0) * { * return false; * } * else * { * return true; * } * * // MATCH: myName, my0Name, myName0, MyName` * // NOT MATCH: 0myName, my Name * //Regex re = new Regex("^[^0-9\\.][a-zA-Z0-9\\.\\:]+[\\`]?$"); * //return re.IsMatch(name); * } */ /* * public static bool IsValidOrEmptyXmlName(string name) * { * if (string.IsNullOrEmpty(name)) * { * return true; * } * else * { * return (IsValidXmlName(name)); * } * }*/ /// <summary> /// Determines whether property should be validated. /// /// For property model element, parent element is not available. However, we should prevent prop.IsDefaultCollection being set /// when parent is not collection. Therefore, we presume that IsDefaultCollection setr to true means parent is collection. /// </summary> /// <param name="val"></param> /// <param name="prop"></param> /// <param name="options"></param> /// <returns></returns> internal static bool RequiresValidation(string val, ConfigurationProperty prop, out NamingHelper.ValidationOptions options) { return(RequiresValidation(val, prop.IsDefaultCollection, prop, out options)); }