internal static ValidationAttribute GetValidatorInstanceForXmlDataAnnotationsValidator(XmlDataAnnotationsValidator validator) { if (validator == null) { return(null); } ValidationAttribute attr = null; if (validator.ValidatorType == typeof(StringLengthAttribute)) { attr = (ValidationAttribute) new StringLengthAttribute(int.Parse(validator.ValidatorAttributes["maximumLength"])); } else if (validator.ValidatorType == typeof(RangeAttribute)) { attr = (ValidationAttribute) new RangeAttribute(double.Parse(validator.ValidatorAttributes["minimum"]), double.Parse(validator.ValidatorAttributes["maximum"])); } else if (validator.ValidatorType == typeof(RegularExpressionAttribute)) { attr = (ValidationAttribute) new RegularExpressionAttribute(validator.ValidatorAttributes["pattern"]); } else { try { attr = (ValidationAttribute)Activator.CreateInstance(validator.ValidatorType); } catch (Exception ex) { throw new Exception("Error trying to create an instance of [" + validator.ValidatorType.Name + "].\r\nIf this validator already has a parameterless constructor defined, see InnerException for more information.", ex); } string currentProperty = ""; try { foreach (KeyValuePair <string, string> kvp in validator.ValidatorAttributes) { currentProperty = kvp.Key; PropertyInfo pi = attr.GetType().GetProperty(kvp.Key); pi.SetValue(attr, Convert.ChangeType(kvp.Value, pi.PropertyType), null); } } catch (Exception ex) { throw new Exception("Error setting properties from XML configuration, current property [" + currentProperty + "]. InnerException may have more details.", ex); } } if (!String.IsNullOrEmpty(validator.ErrorMessage)) { attr.ErrorMessage = validator.ErrorMessage; } if (!String.IsNullOrEmpty(validator.ErrorMessageResourceName)) { attr.ErrorMessageResourceName = validator.ErrorMessageResourceName; } if (validator.ErrorMessageResourceType != null) { attr.ErrorMessageResourceType = validator.ErrorMessageResourceType; } return(attr); }
/// <summary> /// Load the specified XML file into memory as one or more XML rule sets in place of DataAnnotations.<br/> /// Use GetRuleSetForName() and GetRuleSetForType() to retrieve a rule set for validation.<br/> /// The WFXmlRuleSetRuleProvider class with the TryValidateModel() method. /// </summary> public static void RegisterXMLValidationConfiguration(string filename) { XmlDocument doc = new XmlDocument(); doc.Load(filename); foreach (XmlNode node in doc.SelectNodes("validation/type")) { string assemblyName = node.Attributes["assemblyName"].Value; string name = node.Attributes["name"].Value; foreach (XmlNode ruleSetNode in node.SelectNodes("ruleset")) { XmlDataAnnotationsRuleSet ruleset = new XmlDataAnnotationsRuleSet(); try { ruleset.ModelType = Type.GetType(name, true, true); } catch (Exception ex) { throw new Exception("Couldn't resolve model type " + name + ". You may need to specify the fully qualified assembly name in the 'name' field. ie: 'MyAssembly.MyClass, MyAssembly'"); } ruleset.AssemblyName = assemblyName; ruleset.TypeName = name; ruleset.Properties = new List <XmlDataAnnotationsRuleSetProperty>(); ruleset.RuleSetName = ruleSetNode.Attributes["name"].Value; XmlAttribute rattr = ruleSetNode.Attributes["DisplayName"]; ruleset.ModelDisplayName = rattr == null ? ruleset.ModelDisplayName : rattr.Value; XmlNode classAttsNode = ruleSetNode.SelectSingleNode("attributes"); if (classAttsNode != null) { ruleset.ClassValidators = new List <XmlDataAnnotationsValidator>(); foreach (XmlNode classAttr in classAttsNode.SelectNodes("validator")) { XmlDataAnnotationsValidator val = new XmlDataAnnotationsValidator(); foreach (XmlAttribute attr in classAttr.Attributes) { if (attr.Name.ToLower() == "negated") { val.Negated = Boolean.Parse(attr.Value); } else if (attr.Name == "type") { val.ValidatorTypeName = attr.Name; try { val.ValidatorType = Type.GetType(attr.Value, true, true); } catch (Exception ex) { throw new Exception("Couldn't resolve validator type " + attr.Value + ". You may need to specify the fully qualified assembly name in the 'type' field. ie: 'MyAssembly.MyClass, MyAssembly'"); } } else { val.ValidatorAttributes.Add(attr.Name, attr.Value); } } ruleset.ClassValidators.Add(val); } } foreach (XmlNode propertyNode in ruleSetNode.SelectNodes("properties/property")) { XmlDataAnnotationsRuleSetProperty prop = new XmlDataAnnotationsRuleSetProperty(); prop.ParentRuleSet = ruleset; prop.PropertyName = propertyNode.Attributes["name"].Value; XmlAttribute xattr = propertyNode.Attributes["DisplayName"]; if (xattr != null) { prop.DisplayName = xattr.Value; } XmlAttribute rattrResource = propertyNode.Attributes["DisplayNameResourceName"]; XmlAttribute rattrResourceType = propertyNode.Attributes["DisplayNameResourceType"]; if (rattrResource != null) { prop.DisplayNameResourceName = rattrResource.Value; } if (rattrResourceType != null) { try { prop.DisplayNameResourceType = Type.GetType(rattrResourceType.Value, true, true); } catch (Exception ex) { throw new Exception("Couldn't resolve resource type " + rattrResourceType.Value + ". You may need to specify the fully qualified assembly name in the 'type' field. ie: 'MyAssembly.MyClass, MyAssembly'"); } } //Derive from propertyname if a display name was not found if (xattr == null && rattrResource == null && rattrResourceType == null) { prop.DisplayName = prop.PropertyName; } prop.Validators = new List <XmlDataAnnotationsValidator>(); foreach (XmlNode validatorNode in propertyNode.SelectNodes("validator")) { XmlDataAnnotationsValidator validator = new XmlDataAnnotationsValidator(); validator.ParentProperty = prop; foreach (XmlAttribute attr in validatorNode.Attributes) { if (attr.Name == "ErrorMessageResourceName") { validator.ErrorMessageResourceName = attr.Value; } else if (attr.Name == "ErrorMessageResourceType") { try { validator.ErrorMessageResourceType = Type.GetType(attr.Value, true, true); } catch (Exception ex) { throw new Exception("Couldn't resolve resource type " + attr.Value + ". You may need to specify the fully qualified assembly name in the 'type' field. ie: 'MyAssembly.MyClass, MyAssembly'"); } } else if (attr.Name == "ErrorMessage") { validator.ErrorMessage = attr.Value; } else if (attr.Name.ToLower() == "negated") { validator.Negated = Boolean.Parse(attr.Value); } else if (attr.Name == "type") { validator.ValidatorTypeName = attr.Name; if (attr.Value == "RequiredAttribute") { validator.ValidatorType = typeof(RequiredAttribute); } else if (attr.Value == "StringLengthAttribute") { validator.ValidatorType = typeof(StringLengthAttribute); } else if (attr.Value == "RegularExpressionAttribute") { validator.ValidatorType = typeof(RegularExpressionAttribute); } else if (attr.Value == "RangeAttribute") { validator.ValidatorType = typeof(RangeAttribute); } else { try { validator.ValidatorType = Type.GetType(attr.Value, true, true); } catch (Exception ex) { throw new Exception("Couldn't resolve validator type " + attr.Value + ". You may need to specify the fully qualified assembly name in the 'type' field. ie: 'MyAssembly.MyClass, MyAssembly'"); } } } else { validator.ValidatorAttributes.Add(attr.Name, attr.Value); } } prop.Validators.Add(validator); } ruleset.Properties.Add(prop); } XmlRuleSets.Add(ruleset); } } }