public void Validate(object target, Notification notification) { if (!_match.Matches(target)) { return; } var attribute = new GreaterThanZeroAttribute(); attribute.Property = _property.InnerProperty; attribute.Validate(target, notification); }
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)); } } } } } }
public void GreaterThanZeroAttribute_PassSomethingWhenInvalid_ErrorMessageIsProperlyFormatted() { var greaterThanZero = new GreaterThanZeroAttribute(); Assert.AreEqual("field must be greater than zero",greaterThanZero.FormatErrorMessage("field")); }
public void GreaterThanZeroAttribute_PassSomethingNotNumeric_ReturnsFalse() { var greaterThanZero = new GreaterThanZeroAttribute(); Assert.IsFalse(greaterThanZero.IsValid("a")); }
public void GreaterThanZeroAttribute_PassSomethingEqualOrLessThanZeroToIsValid_ReturnsFalse() { var greaterThanZero = new GreaterThanZeroAttribute(); Assert.IsFalse(greaterThanZero.IsValid(0)); Assert.IsFalse(greaterThanZero.IsValid(-1)); }
public void GreaterThanZeroAttribute_PassSomethingGreaterThanZeroToIsValid_ReturnsTrue() { var greaterThanZero = new GreaterThanZeroAttribute(); Assert.IsTrue(greaterThanZero.IsValid(1)); }