Inheritance: System.ComponentModel.DataAnnotations.ValidationAttribute
        public void GetClientValidationRules_ReturnsEqualToValidationRule()
        {
            ModelMetadata metadata = new DataAnnotationsModelMetadataProvider().GetMetadataForProperty(null, typeof(AdaptersModel), "EqualTo");
            EqualToAdapter adapter = new EqualToAdapter(metadata, new ControllerContext(), new EqualToAttribute("StringLength"));

            String expectedMessage = new EqualToAttribute("StringLength").FormatErrorMessage(metadata.GetDisplayName());
            ModelClientValidationRule actual = adapter.GetClientValidationRules().Single();

            Assert.Equal("*.StringLength", actual.ValidationParameters["other"]);
            Assert.Equal(expectedMessage, actual.ErrorMessage);
            Assert.Equal("equalto", actual.ValidationType);
            Assert.Single(actual.ValidationParameters);
        }
        public void GetClientValidationRules_SetsOtherPropertyDisplayName()
        {
            ModelMetadata metadata = new DataAnnotationsModelMetadataProvider().GetMetadataForProperty(null, typeof(AdaptersModel), "EqualTo");
            EqualToAttribute attribute = new EqualToAttribute("StringLength");
            attribute.OtherPropertyDisplayName = null;

            new EqualToAdapter(metadata, new ControllerContext(), attribute).GetClientValidationRules();

            String expected = ResourceProvider.GetPropertyTitle(typeof(AdaptersModel), "EqualTo");
            String actual = attribute.OtherPropertyDisplayName;

            Assert.Equal(expected, actual);
        }
Exemple #3
0
 public IAttributeAdapter?GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer?stringLocalizer)
 {
     return(attribute switch
     {
         StringLengthAttribute stringLength => new StringLengthAdapter(stringLength),
         GreaterThanAttribute greaterThan => new GreaterThanAdapter(greaterThan),
         AcceptFilesAttribute acceptFiles => new AcceptFilesAdapter(acceptFiles),
         MinLengthAttribute minLength => new MinLengthAdapter(minLength),
         EmailAddressAttribute email => new EmailAddressAdapter(email),
         RequiredAttribute required => new RequiredAdapter(required),
         MaxValueAttribute maxValue => new MaxValueAdapter(maxValue),
         MinValueAttribute minValue => new MinValueAdapter(minValue),
         LessThanAttribute lessThan => new LessThanAdapter(lessThan),
         FileSizeAttribute fileSize => new FileSizeAdapter(fileSize),
         EqualToAttribute equalTo => new EqualToAdapter(equalTo),
         IntegerAttribute integer => new IntegerAdapter(integer),
         DigitsAttribute digits => new DigitsAdapter(digits),
         NumberAttribute number => new NumberAdapter(number),
         RangeAttribute range => new RangeAdapter(range),
         _ => null
     });