public void ClientRulesWithCompareAttribute_ErrorMessageUsesDisplayName() { // Arrange var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = metadataProvider.GetMetadataForProperty(typeof(PropertyDisplayNameModel), "MyProperty"); var attribute = new CompareAttribute("OtherProperty"); attribute.ErrorMessage = "CompareAttributeErrorMessage"; var stringLocalizer = new Mock<IStringLocalizer>(); var expectedProperties = new object[] { "MyPropertyDisplayName", "OtherPropertyDisplayName" }; var expectedMessage = "'MyPropertyDisplayName' and 'OtherPropertyDisplayName' do not match."; stringLocalizer.Setup(s => s[attribute.ErrorMessage, expectedProperties]) .Returns(new LocalizedString(attribute.ErrorMessage, expectedMessage)); var adapter = new CompareAttributeAdapter(attribute, stringLocalizer: stringLocalizer.Object); var actionContext = new ActionContext(); var context = new ClientModelValidationContext(actionContext, metadata, metadataProvider); // Act var rules = adapter.GetClientValidationRules(context); // Assert var rule = Assert.Single(rules); // Mono issue - https://github.com/aspnet/External/issues/19 Assert.Equal(expectedMessage, rule.ErrorMessage); }
public void ClientRulesWithCompareAttribute_ErrorMessageUsesDisplayName() { // Arrange var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = metadataProvider.GetMetadataForProperty(typeof(PropertyDisplayNameModel), "MyProperty"); var attribute = new CompareAttribute("OtherProperty"); var adapter = new CompareAttributeAdapter(attribute, stringLocalizer: null); var serviceCollection = new ServiceCollection(); var requestServices = serviceCollection.BuildServiceProvider(); var context = new ClientModelValidationContext(metadata, metadataProvider, requestServices); // Act var rules = adapter.GetClientValidationRules(context); // Assert var rule = Assert.Single(rules); // Mono issue - https://github.com/aspnet/External/issues/19 Assert.Equal( PlatformNormalizer.NormalizeContent( "'MyPropertyDisplayName' and 'OtherPropertyDisplayName' do not match."), rule.ErrorMessage); }
public void AddValidation_DoesNotTrounceExistingAttributes() { // Arrange var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = metadataProvider.GetMetadataForProperty(typeof(PropertyNameModel), "MyProperty"); var attribute = new CompareAttribute("OtherProperty"); var adapter = new CompareAttributeAdapter(attribute, stringLocalizer: null); var actionContext = new ActionContext(); var context = new ClientModelValidationContext( actionContext, metadata, metadataProvider, new AttributeDictionary()); context.Attributes.Add("data-val", "original"); context.Attributes.Add("data-val-equalto", "original"); context.Attributes.Add("data-val-equalto-other", "original"); // Act adapter.AddValidation(context); // Assert Assert.Collection( context.Attributes, kvp => { Assert.Equal("data-val", kvp.Key); Assert.Equal("original", kvp.Value); }, kvp => { Assert.Equal("data-val-equalto", kvp.Key); Assert.Equal("original", kvp.Value); }, kvp => { Assert.Equal("data-val-equalto-other", kvp.Key); Assert.Equal("original", kvp.Value); }); }
public void ClientRulesWithCompareAttribute_ErrorMessageUsesResourceOverride() { // Arrange ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForProperty( () => null, typeof(PropertyNameModel), "MyProperty" ); ControllerContext context = new ControllerContext(); AnnotationsCompareAttribute attribute = new AnnotationsCompareAttribute("OtherProperty") { ErrorMessageResourceName = "CompareAttributeTestResource", ErrorMessageResourceType = typeof(MyResources), }; ModelValidator adapter = new CompareAttributeAdapter(metadata, context, attribute); // Act ModelClientValidationRule[] rules = adapter .GetClientValidationRules() .OrderBy(r => r.ValidationType) .ToArray(); // Assert ModelClientValidationRule rule = Assert.Single(rules); Assert.Equal("Hello 'MyProperty', goodbye 'OtherProperty'.", rule.ErrorMessage); }
public void AddValidation_DoesNotTrounceExistingAttributes() { // Arrange var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = metadataProvider.GetMetadataForProperty(typeof(PropertyNameModel), "MyProperty"); var attribute = new CompareAttribute("OtherProperty"); var adapter = new CompareAttributeAdapter(attribute, stringLocalizer: null); var actionContext = new ActionContext(); var context = new ClientModelValidationContext( actionContext, metadata, metadataProvider, new Dictionary <string, string>()); context.Attributes.Add("data-val", "original"); context.Attributes.Add("data-val-equalto", "original"); context.Attributes.Add("data-val-equalto-other", "original"); // Act adapter.AddValidation(context); // Assert Assert.Collection( context.Attributes, kvp => { Assert.Equal("data-val", kvp.Key); Assert.Equal("original", kvp.Value); }, kvp => { Assert.Equal("data-val-equalto", kvp.Key); Assert.Equal("original", kvp.Value); }, kvp => { Assert.Equal("data-val-equalto-other", kvp.Key); Assert.Equal("original", kvp.Value); }); }
public void ClientRulesWithCompareAttribute_ErrorMessageUsesPropertyName() { // Arrange var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = metadataProvider.GetMetadataForProperty(typeof(PropertyNameModel), "MyProperty"); var attribute = new CompareAttribute("OtherProperty"); var adapter = new CompareAttributeAdapter(attribute, stringLocalizer: null); var expectedMessage = "'MyProperty' and 'OtherProperty' do not match."; var actionContext = new ActionContext(); var context = new ClientModelValidationContext( actionContext, metadata, metadataProvider, new Dictionary <string, string>()); // Act adapter.AddValidation(context); // Assert Assert.Collection( context.Attributes, kvp => { Assert.Equal("data-val", kvp.Key); Assert.Equal("true", kvp.Value); }, kvp => { Assert.Equal("data-val-equalto", kvp.Key); Assert.Equal(expectedMessage, kvp.Value); }, kvp => { Assert.Equal("data-val-equalto-other", kvp.Key); Assert.Equal("*.OtherProperty", kvp.Value); }); }
public void ClientRulesWithCompareAttribute_ErrorMessageUsesPropertyName() { // Arrange var metadataProvider = new DataAnnotationsModelMetadataProvider(); var metadata = metadataProvider.GetMetadataForProperty(() => null, typeof(PropertyNameModel), "MyProperty"); var attribute = new CompareAttribute("OtherProperty"); var context = new ClientModelValidationContext(metadata, metadataProvider); var adapter = new CompareAttributeAdapter(attribute); // Act var rules = adapter.GetClientValidationRules(context); // Assert var rule = Assert.Single(rules); Assert.Equal("'MyProperty' and 'OtherProperty' do not match.", rule.ErrorMessage); }
public void AddValidation_adds_confirmed_rule() { // Arrange var attribute = new CompareAttribute("PropertyName"); var adapter = new CompareAttributeAdapter(attribute); var context = new ClientModelValidationContextBuilder() .WithModelType <string>() .Build(); // Act adapter.AddValidation(context); // Assert context.Attributes.Keys.ShouldContain("v-validate"); context.Attributes["v-validate"].ShouldBe("{confirmed:'PropertyName'}"); }
public void ClientRulesWithCompareAttribute_ErrorMessageUsesPropertyName() { // Arrange var metadata = ModelMetadataProviders.Current.GetMetadataForProperty(() => null, typeof(PropertyNameModel), "MyProperty"); var context = new ControllerContext(); var attribute = new System.ComponentModel.DataAnnotations.CompareAttribute("OtherProperty"); var adapter = new CompareAttributeAdapter(metadata, context, attribute); // Act var rules = adapter.GetClientValidationRules() .OrderBy(r => r.ValidationType) .ToArray(); // Assert ModelClientValidationRule rule = Assert.Single(rules); Assert.Equal("'MyProperty' and 'OtherProperty' do not match.", rule.ErrorMessage); }
public void ClientRulesWithCompareAttribute_ErrorMessageUsesPropertyName() { // Arrange var metadata = ModelMetadataProviders.Current.GetMetadataForProperty(() => null, typeof(PropertyNameModel), "MyProperty"); var context = new ControllerContext(); var attribute = new AnnotationsCompareAttribute("OtherProperty"); var adapter = new CompareAttributeAdapter(metadata, context, attribute); // Act var rules = adapter.GetClientValidationRules() .OrderBy(r => r.ValidationType) .ToArray(); // Assert ModelClientValidationRule rule = Assert.Single(rules); Assert.Equal("'MyProperty' and 'OtherProperty' do not match.", rule.ErrorMessage); }
public void ClientRulesWithCompareAttribute_ErrorMessageUsesPropertyName() { // Arrange var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = metadataProvider.GetMetadataForProperty(typeof(PropertyNameModel), "MyProperty"); var attribute = new CompareAttribute("OtherProperty"); var serviceCollection = new ServiceCollection(); var requestServices = serviceCollection.BuildServiceProvider(); var context = new ClientModelValidationContext(metadata, metadataProvider, requestServices); var adapter = new CompareAttributeAdapter(attribute); // Act var rules = adapter.GetClientValidationRules(context); // Assert var rule = Assert.Single(rules); Assert.Equal("'MyProperty' and 'OtherProperty' do not match.", rule.ErrorMessage); }
public void ClientRulesWithCompareAttribute_ErrorMessageUsesResourceOverride() { // Arrange var metadataProvider = new DataAnnotationsModelMetadataProvider(); var metadata = metadataProvider.GetMetadataForProperty(() => null, typeof(PropertyNameModel), "MyProperty"); var attribute = new CompareAttribute("OtherProperty") { ErrorMessageResourceName = "CompareAttributeTestResource", ErrorMessageResourceType = typeof(Test.Resources), }; var context = new ClientModelValidationContext(metadata, metadataProvider); var adapter = new CompareAttributeAdapter(attribute); // Act var rules = adapter.GetClientValidationRules(context); // Assert var rule = Assert.Single(rules); Assert.Equal("Comparing MyProperty to OtherProperty.", rule.ErrorMessage); }
public void ClientRulesWithCompareAttribute_ErrorMessageUsesOverride() { // Arrange ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForProperty(() => null, typeof(PropertyNameModel), "MyProperty"); ControllerContext context = new ControllerContext(); AnnotationsCompareAttribute attribute = new AnnotationsCompareAttribute("OtherProperty") { ErrorMessage = "Hello '{0}', goodbye '{1}'.", }; ModelValidator adapter = new CompareAttributeAdapter(metadata, context, attribute); // Act ModelClientValidationRule[] rules = adapter.GetClientValidationRules() .OrderBy(r => r.ValidationType) .ToArray(); // Assert ModelClientValidationRule rule = Assert.Single(rules); Assert.Equal("Hello 'MyProperty', goodbye 'OtherProperty'.", rule.ErrorMessage); }
public void ClientRulesWithCompareAttribute_ErrorMessageUsesDisplayName() { // Arrange var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = metadataProvider.GetMetadataForProperty(typeof(PropertyDisplayNameModel), "MyProperty"); var attribute = new CompareAttribute("OtherProperty"); attribute.ErrorMessage = "CompareAttributeErrorMessage"; var stringLocalizer = new Mock <IStringLocalizer>(); var expectedProperties = new object[] { "MyPropertyDisplayName", "OtherPropertyDisplayName" }; var expectedMessage = "'MyPropertyDisplayName' and 'OtherPropertyDisplayName' do not match."; stringLocalizer.Setup(s => s[attribute.ErrorMessage, expectedProperties]) .Returns(new LocalizedString(attribute.ErrorMessage, expectedMessage)); var adapter = new CompareAttributeAdapter(attribute, stringLocalizer: stringLocalizer.Object); var actionContext = new ActionContext(); var context = new ClientModelValidationContext( actionContext, metadata, metadataProvider, new Dictionary <string, string>()); // Act adapter.AddValidation(context); // Assert Assert.Collection( context.Attributes, kvp => { Assert.Equal("data-val", kvp.Key); Assert.Equal("true", kvp.Value); }, kvp => { Assert.Equal("data-val-equalto", kvp.Key); Assert.Equal(expectedMessage, kvp.Value); }, kvp => { Assert.Equal("data-val-equalto-other", kvp.Key); Assert.Equal("*.OtherProperty", kvp.Value); }); }
public void ClientRulesWithCompareAttribute_ErrorMessageUsesOverride() { // Arrange var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = metadataProvider.GetMetadataForProperty( typeof(PropertyNameModel), "MyProperty"); var attribute = new CompareAttribute("OtherProperty") { ErrorMessage = "Hello '{0}', goodbye '{1}'." }; var serviceCollection = new ServiceCollection(); var requestServices = serviceCollection.BuildServiceProvider(); var context = new ClientModelValidationContext(metadata, metadataProvider, requestServices); var adapter = new CompareAttributeAdapter(attribute, stringLocalizer: null); // Act var rules = adapter.GetClientValidationRules(context); // Assert var rule = Assert.Single(rules); Assert.Equal("Hello 'MyProperty', goodbye 'OtherProperty'.", rule.ErrorMessage); }
public void ClientRulesWithCompareAttribute_ErrorMessageUsesDisplayName() { // Arrange var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = metadataProvider.GetMetadataForProperty(typeof(PropertyDisplayNameModel), "MyProperty"); var attribute = new CompareAttribute("OtherProperty"); attribute.ErrorMessage = "CompareAttributeErrorMessage"; var stringLocalizer = new Mock<IStringLocalizer>(); var expectedProperties = new object[] { "MyPropertyDisplayName", "OtherPropertyDisplayName" }; var expectedMessage = "'MyPropertyDisplayName' and 'OtherPropertyDisplayName' do not match."; stringLocalizer.Setup(s => s[attribute.ErrorMessage, expectedProperties]) .Returns(new LocalizedString(attribute.ErrorMessage, expectedMessage)); var adapter = new CompareAttributeAdapter(attribute, stringLocalizer: stringLocalizer.Object); var actionContext = new ActionContext(); var context = new ClientModelValidationContext( actionContext, metadata, metadataProvider, new AttributeDictionary()); // Act adapter.AddValidation(context); // Assert Assert.Collection( context.Attributes, kvp => { Assert.Equal("data-val", kvp.Key); Assert.Equal("true", kvp.Value); }, kvp => { Assert.Equal("data-val-equalto", kvp.Key); Assert.Equal(expectedMessage, kvp.Value); }, kvp => { Assert.Equal("data-val-equalto-other", kvp.Key); Assert.Equal(kvp.Value, "*.OtherProperty"); }); }
public void ClientRulesWithCompareAttribute_ErrorMessageUsesPropertyName() { // Arrange var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = metadataProvider.GetMetadataForProperty(typeof(PropertyNameModel), "MyProperty"); var attribute = new CompareAttribute("OtherProperty"); var adapter = new CompareAttributeAdapter(attribute, stringLocalizer: null); // Mono issue - https://github.com/aspnet/External/issues/19 var expectedMessage = PlatformNormalizer.NormalizeContent("'MyProperty' and 'OtherProperty' do not match."); var actionContext = new ActionContext(); var context = new ClientModelValidationContext( actionContext, metadata, metadataProvider, new AttributeDictionary()); // Act adapter.AddValidation(context); // Assert Assert.Collection( context.Attributes, kvp => { Assert.Equal("data-val", kvp.Key); Assert.Equal("true", kvp.Value); }, kvp => { Assert.Equal("data-val-equalto", kvp.Key); Assert.Equal(expectedMessage, kvp.Value); }, kvp => { Assert.Equal("data-val-equalto-other", kvp.Key); Assert.Equal(kvp.Value, "*.OtherProperty"); }); }
public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer) { if (attribute == null) { throw new ArgumentNullException(nameof(attribute)); } if (_options == null) { throw new ArgumentNullException(nameof(_options)); } IAttributeAdapter adapter; if (attribute is CompareAttribute compareAttribute) { adapter = new CompareAttributeAdapter(compareAttribute); } else if (attribute is CreditCardAttribute creditCardAttribute) { adapter = new CreditCardAttributeAdapter(creditCardAttribute); } else if (attribute is EmailAddressAttribute emailAddressAttribute) { adapter = new EmailAddressAttributeAdapter(emailAddressAttribute); } else if (attribute is FileExtensionsAttribute fileExtensionsAttribute) { adapter = new FileExtensionsAttributeAdapter(fileExtensionsAttribute); } else if (attribute is MaxLengthAttribute maxLengthAttribute) { adapter = new MaxLengthAttributeAdapter(maxLengthAttribute); } else if (attribute is MinLengthAttribute minLengthAttribute) { adapter = new MinLengthAttributeAdapter(minLengthAttribute); } else if (attribute is RangeAttribute rangeAttribute) { adapter = new RangeAttributeAdapter(rangeAttribute, _options); } else if (attribute is RegularExpressionAttribute regularExpressionAttribute) { adapter = new RegularExpressionAttributeAdapter(regularExpressionAttribute); } else if (attribute is RequiredAttribute requiredAttribute) { adapter = new RequiredAttributeAdapter(requiredAttribute); } else if (attribute is StringLengthAttribute stringLengthAttribute) { adapter = new StringLengthAttributeAdapter(stringLengthAttribute); } else if (attribute is UrlAttribute urlAttribute) { adapter = new UrlAttributeAdapter(urlAttribute); } else { adapter = null; } return(adapter); }
public void ClientRulesWithCompareAttribute_ErrorMessageUsesResourceOverride() { // Arrange var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = metadataProvider.GetMetadataForProperty(typeof(PropertyNameModel), "MyProperty"); var attribute = new CompareAttribute("OtherProperty") { ErrorMessageResourceName = "CompareAttributeTestResource", ErrorMessageResourceType = typeof(DataAnnotations.Test.Resources), }; var adapter = new CompareAttributeAdapter(attribute, stringLocalizer: null); var expectedMessage = "Comparing MyProperty to OtherProperty."; var actionContext = new ActionContext(); var context = new ClientModelValidationContext( actionContext, metadata, metadataProvider, new AttributeDictionary()); // Act adapter.AddValidation(context); // Assert Assert.Collection( context.Attributes, kvp => { Assert.Equal("data-val", kvp.Key); Assert.Equal("true", kvp.Value); }, kvp => { Assert.Equal("data-val-equalto", kvp.Key); Assert.Equal(expectedMessage, kvp.Value); }, kvp => { Assert.Equal("data-val-equalto-other", kvp.Key); Assert.Equal(kvp.Value, "*.OtherProperty"); }); }
public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer) { if (attribute == null) { throw new ArgumentNullException(nameof(attribute)); } IAttributeAdapter adapter; var type = attribute.GetType(); if (attribute is RegularExpressionAttribute) { adapter = new RegularExpressionAttributeAdapter((RegularExpressionAttribute)attribute, stringLocalizer); } else if (attribute is MaxLengthAttribute) { adapter = new MaxLengthAttributeAdapter((MaxLengthAttribute)attribute, stringLocalizer); } else if (attribute is RequiredAttribute) { adapter = new RequiredAttributeAdapter((RequiredAttribute)attribute, stringLocalizer); } else if (attribute is CompareAttribute) { adapter = new CompareAttributeAdapter((CompareAttribute)attribute, stringLocalizer); } else if (attribute is MinLengthAttribute) { adapter = new MinLengthAttributeAdapter((MinLengthAttribute)attribute, stringLocalizer); } else if (attribute is CreditCardAttribute) { adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-creditcard", stringLocalizer); } else if (attribute is StringLengthAttribute) { adapter = new StringLengthAttributeAdapter((StringLengthAttribute)attribute, stringLocalizer); } else if (attribute is RangeAttribute) { adapter = new RangeAttributeAdapter((RangeAttribute)attribute, stringLocalizer); } else if (attribute is EmailAddressAttribute) { adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-email", stringLocalizer); } else if (attribute is PhoneAttribute) { adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-phone", stringLocalizer); } else if (attribute is UrlAttribute) { adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-url", stringLocalizer); } else if (attribute is FileExtensionsAttribute) { adapter = new FileExtensionsAttributeAdapter((FileExtensionsAttribute)attribute, stringLocalizer); } else { adapter = defaultClientModelValidatorProvider.GetAttributeAdapter(attribute, stringLocalizer); } return(adapter); }
/// <summary> /// Creates an <see cref="IAttributeAdapter"/> for the given attribute. /// </summary> /// <param name="attribute">The attribute to create an adapter for.</param> /// <param name="stringLocalizer">The localizer to provide to the adapter.</param> /// <returns>An <see cref="IAttributeAdapter"/> for the given attribute.</returns> public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer) { if (attribute == null) { throw new ArgumentNullException(nameof(attribute)); } IAttributeAdapter adapter; var type = attribute.GetType(); if (type == typeof(RegularExpressionAttribute)) { adapter = new RegularExpressionAttributeAdapter((RegularExpressionAttribute)attribute, stringLocalizer); } else if (type == typeof(MaxLengthAttribute)) { adapter = new MaxLengthAttributeAdapter((MaxLengthAttribute)attribute, stringLocalizer); } else if (type == typeof(RequiredAttribute)) { adapter = new RequiredAttributeAdapter((RequiredAttribute)attribute, stringLocalizer); } else if (type == typeof(CompareAttribute)) { adapter = new CompareAttributeAdapter((CompareAttribute)attribute, stringLocalizer); } else if (type == typeof(MinLengthAttribute)) { adapter = new MinLengthAttributeAdapter((MinLengthAttribute)attribute, stringLocalizer); } else if (type == typeof(CreditCardAttribute)) { adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "creditcard", stringLocalizer); } else if (type == typeof(StringLengthAttribute)) { adapter = new StringLengthAttributeAdapter((StringLengthAttribute)attribute, stringLocalizer); } else if (type == typeof(RangeAttribute)) { adapter = new RangeAttributeAdapter((RangeAttribute)attribute, stringLocalizer); } else if (type == typeof(EmailAddressAttribute)) { adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "email", stringLocalizer); } else if (type == typeof(PhoneAttribute)) { adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "phone", stringLocalizer); } else if (type == typeof(UrlAttribute)) { adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "url", stringLocalizer); } else { adapter = null; } return adapter; }
public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer) { if (attribute == null) { throw new ArgumentNullException(nameof(attribute)); } IAttributeAdapter adapter = null; var type = attribute.GetType(); #if NETCOREAPP2_2 if (attribute is RegularExpressionAttribute) { adapter = new RegularExpressionAttributeAdapter((RegularExpressionAttribute)attribute, stringLocalizer); } else if (attribute is MaxLengthAttribute) { adapter = new MaxLengthAttributeAdapter((MaxLengthAttribute)attribute, stringLocalizer); } else if (attribute is RequiredAttribute) { adapter = new Microsoft.AspNetCore.Mvc.DataAnnotations.Internal.RequiredAttributeAdapter((RequiredAttribute)attribute, stringLocalizer); } else if (attribute is CompareAttribute) { adapter = new CompareAttributeAdapter((CompareAttribute)attribute, stringLocalizer); } else if (attribute is MinLengthAttribute) { adapter = new MinLengthAttributeAdapter((MinLengthAttribute)attribute, stringLocalizer); } else if (attribute is CreditCardAttribute) { adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-creditcard", stringLocalizer); } else if (attribute is StringLengthAttribute) { adapter = new StringLengthAttributeAdapter((StringLengthAttribute)attribute, stringLocalizer); } else if (attribute is RangeAttribute) { adapter = new RangeAttributeAdapter((RangeAttribute)attribute, stringLocalizer); } else if (attribute is EmailAddressAttribute) { adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-email", stringLocalizer); } else if (attribute is PhoneAttribute) { adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-phone", stringLocalizer); } else if (attribute is UrlAttribute) { adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-url", stringLocalizer); } else if (attribute is FileExtensionsAttribute) { adapter = new FileExtensionsAttributeAdapter((FileExtensionsAttribute)attribute, stringLocalizer); } else { adapter = defaultClientModelValidatorProvider.GetAttributeAdapter(attribute, stringLocalizer); } #else if (attribute is RegularExpressionAttribute) { adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.RegularExpressionAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (RegularExpressionAttribute)attribute, stringLocalizer); } else if (attribute is MaxLengthAttribute) { adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.MaxLengthAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (MaxLengthAttribute)attribute, stringLocalizer); } else if (attribute is RequiredAttribute) { adapter = new RequiredAttributeAdapter((RequiredAttribute)attribute, stringLocalizer); } else if (attribute is CompareAttribute) { adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.CompareAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (CompareAttribute)attribute, stringLocalizer); } else if (attribute is MinLengthAttribute) { adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.MinLengthAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (MinLengthAttribute)attribute, stringLocalizer); } else if (attribute is CreditCardAttribute) { adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.DataTypeAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (DataTypeAttribute)attribute, "data-val-creditcard", stringLocalizer); } else if (attribute is StringLengthAttribute) { adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.StringLengthAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (StringLengthAttribute)attribute, stringLocalizer); } else if (attribute is RangeAttribute) { adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.RangeAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (RangeAttribute)attribute, stringLocalizer); } else if (attribute is EmailAddressAttribute) { adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.DataTypeAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (DataTypeAttribute)attribute, "data-val-email", stringLocalizer); } else if (attribute is PhoneAttribute) { adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.DataTypeAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (DataTypeAttribute)attribute, "data-val-phone", stringLocalizer); } else if (attribute is UrlAttribute) { adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.DataTypeAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (DataTypeAttribute)attribute, "data-val-url", stringLocalizer); } else if (attribute is FileExtensionsAttribute) { adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.FileExtensionsAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (FileExtensionsAttribute)attribute, stringLocalizer); } else { adapter = defaultClientModelValidatorProvider.GetAttributeAdapter(attribute, stringLocalizer); } #endif return(adapter); }
public void ClientRulesWithCompareAttribute_ErrorMessageUsesResourceOverride() { // Arrange var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = metadataProvider.GetMetadataForProperty(typeof(PropertyNameModel), "MyProperty"); var attribute = new CompareAttribute("OtherProperty") { ErrorMessageResourceName = "CompareAttributeTestResource", ErrorMessageResourceType = typeof(DataAnnotations.Test.Resources), }; var serviceCollection = new ServiceCollection(); var requestServices = serviceCollection.BuildServiceProvider(); var context = new ClientModelValidationContext(metadata, metadataProvider, requestServices); var adapter = new CompareAttributeAdapter(attribute, stringLocalizer: null); // Act var rules = adapter.GetClientValidationRules(context); // Assert var rule = Assert.Single(rules); Assert.Equal("Comparing MyProperty to OtherProperty.", rule.ErrorMessage); }
public void ClientRulesWithCompareAttribute_ErrorMessageUsesResourceOverride() { if (TestPlatformHelper.IsMono) { // ValidationAttribute in Mono does not read non-public resx properties. return; } // Arrange var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = metadataProvider.GetMetadataForProperty(typeof(PropertyNameModel), "MyProperty"); var attribute = new CompareAttribute("OtherProperty") { ErrorMessageResourceName = "CompareAttributeTestResource", ErrorMessageResourceType = typeof(Extensions.Test.Resources), }; var serviceCollection = new ServiceCollection(); var requestServices = serviceCollection.BuildServiceProvider(); var context = new ClientModelValidationContext(metadata, metadataProvider, requestServices); var adapter = new CompareAttributeAdapter(attribute); // Act var rules = adapter.GetClientValidationRules(context); // Assert var rule = Assert.Single(rules); Assert.Equal("Comparing MyProperty to OtherProperty.", rule.ErrorMessage); }
public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer) { if (attribute == null) { throw new ArgumentNullException(nameof(attribute)); } IAttributeAdapter adapter; var type = attribute.GetType(); if (type == typeof(RegularExpressionAttribute)) { adapter = new RegularExpressionAttributeAdapter((RegularExpressionAttribute)attribute, stringLocalizer); } else if (type == typeof(MaxLengthAttribute)) { adapter = new MaxLengthAttributeAdapter((MaxLengthAttribute)attribute, stringLocalizer); } else if (type == typeof(RequiredAttribute)) { adapter = new RequiredAttributeAdapter((RequiredAttribute)attribute, stringLocalizer); } else if (type == typeof(CompareAttribute)) { adapter = new CompareAttributeAdapter((CompareAttribute)attribute, stringLocalizer); } else if (type == typeof(MinLengthAttribute)) { adapter = new MinLengthAttributeAdapter((MinLengthAttribute)attribute, stringLocalizer); } else if (type == typeof(CreditCardAttribute)) { adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-creditcard", stringLocalizer); } else if (type == typeof(StringLengthAttribute)) { adapter = new StringLengthAttributeAdapter((StringLengthAttribute)attribute, stringLocalizer); } else if (type == typeof(RangeAttribute)) { adapter = new RangeAttributeAdapterExt((RangeAttribute)attribute, stringLocalizer); } else if (type == typeof(DynamicRangeAttribute)) { adapter = new DynamicRangeAttributeAdapter((DynamicRangeAttribute)attribute, stringLocalizer); } else if (type == typeof(EmailAddressAttribute)) { adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-email", stringLocalizer); } else if (type == typeof(PhoneAttribute)) { adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-phone", stringLocalizer); } else if (type == typeof(UrlAttribute)) { adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-url", stringLocalizer); } else { adapter = null; } return(adapter); }