private ModelValidator GetValidator(ModelMetadata metadata, ControllerContext context, ValidatorData item) { DataAnnotationsModelValidationFactory factory; if (item.GetType().Name == "NotNullValidatorData") { ValidationAttribute attribute = new RequiredAttribute { ErrorMessage = item.MessageTemplate }; if (AttributeFactories.TryGetValue(attribute.GetType(), out factory)) return factory(metadata, context, attribute); } else if (item.GetType().Name == "RegexValidatorData") { RegexValidatorData regexData = (RegexValidatorData)item; RegularExpressionAttribute regexAttribute = new RegularExpressionAttribute(regexData.Pattern) { ErrorMessage = item.GetMessageTemplate() }; if (AttributeFactories.TryGetValue(regexAttribute.GetType(), out factory)) return factory(metadata, context, regexAttribute); } return null; }
protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes) { List<ModelValidator> vals = base.GetValidators(metadata, context, attributes).ToList(); DataAnnotationsModelValidationFactory factory; // Inject our new validator. if (metadata.ContainerType != null) { // Check if we have validation for this class name. if (ValidationManager.Validators.ContainsKey(metadata.ContainerType.Name)) { var validator = ValidationManager.Validators[metadata.ContainerType.Name]; // Check if we have validation for this property name. if (validator.ContainsKey(metadata.PropertyName)) { var property = validator[metadata.PropertyName]; // Only add validation to visible properties. if (property.Visible) { // Required attribute. if (property.Required) { ValidationAttribute required; if (metadata.ModelType == typeof(bool)) { // For required booleans, enforce true. required = new EnforceTrueAttribute { ErrorMessage = property.ErrorMessage }; } else if (metadata.ModelType == typeof(int) || metadata.ModelType == typeof(long) || metadata.ModelType == typeof(double) || metadata.ModelType == typeof(float)) { // For required int, long, double, float (dropdownlists), enforce > 0. required = new GreaterThanZeroAttribute() { ErrorMessage = property.ErrorMessage }; } else { required = new RequiredAttribute { ErrorMessage = property.ErrorMessage }; } if (!AttributeFactories.TryGetValue(required.GetType(), out factory)) { factory = DefaultAttributeFactory; } yield return factory(metadata, context, required); } // Regular expression attribute. if (!string.IsNullOrEmpty(property.RegularExpression)) { RegularExpressionAttribute regEx = new RegularExpressionAttribute(property.RegularExpression) { ErrorMessage = property.ErrorMessage }; if (!AttributeFactories.TryGetValue(regEx.GetType(), out factory)) { factory = DefaultAttributeFactory; } yield return factory(metadata, context, regEx); } // Compare attribute. if (!string.IsNullOrEmpty(property.Compare)) { CompareAttribute compare = new CompareAttribute(property.Compare) { ErrorMessage = property.ErrorMessage }; if (!AttributeFactories.TryGetValue(compare.GetType(), out factory)) { factory = DefaultAttributeFactory; } yield return factory(metadata, context, compare); } } } } } }