Skip to content

Commit 3ec6f86

Browse files
neilbgrpranavkm
authored andcommitted
ValidationAttributeAdapterProvider allows ValidationAttribute's sub-classes. (#14074)
This permits to inherit built-in ValidationAttribute classes (ie RegularExpressionValidationAttribute, ...) while keeping the standard behavior. Fixes #13782
1 parent 2de6c73 commit 3ec6f86

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

src/Mvc/Mvc.DataAnnotations/src/ValidationAttributeAdapterProvider.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,51 +29,51 @@ public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStr
2929

3030
var type = attribute.GetType();
3131

32-
if (type == typeof(RegularExpressionAttribute))
32+
if (typeof(RegularExpressionAttribute).IsAssignableFrom(type))
3333
{
3434
adapter = new RegularExpressionAttributeAdapter((RegularExpressionAttribute)attribute, stringLocalizer);
3535
}
36-
else if (type == typeof(MaxLengthAttribute))
36+
else if (typeof(MaxLengthAttribute).IsAssignableFrom(type))
3737
{
3838
adapter = new MaxLengthAttributeAdapter((MaxLengthAttribute)attribute, stringLocalizer);
3939
}
40-
else if (type == typeof(RequiredAttribute))
40+
else if (typeof(RequiredAttribute).IsAssignableFrom(type))
4141
{
4242
adapter = new RequiredAttributeAdapter((RequiredAttribute)attribute, stringLocalizer);
4343
}
44-
else if (type == typeof(CompareAttribute))
44+
else if (typeof(CompareAttribute).IsAssignableFrom(type))
4545
{
4646
adapter = new CompareAttributeAdapter((CompareAttribute)attribute, stringLocalizer);
4747
}
48-
else if (type == typeof(MinLengthAttribute))
48+
else if (typeof(MinLengthAttribute).IsAssignableFrom(type))
4949
{
5050
adapter = new MinLengthAttributeAdapter((MinLengthAttribute)attribute, stringLocalizer);
5151
}
52-
else if (type == typeof(CreditCardAttribute))
52+
else if (typeof(CreditCardAttribute).IsAssignableFrom(type))
5353
{
5454
adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-creditcard", stringLocalizer);
5555
}
56-
else if (type == typeof(StringLengthAttribute))
56+
else if (typeof(StringLengthAttribute).IsAssignableFrom(type))
5757
{
5858
adapter = new StringLengthAttributeAdapter((StringLengthAttribute)attribute, stringLocalizer);
5959
}
60-
else if (type == typeof(RangeAttribute))
60+
else if (typeof(RangeAttribute).IsAssignableFrom(type))
6161
{
6262
adapter = new RangeAttributeAdapter((RangeAttribute)attribute, stringLocalizer);
6363
}
64-
else if (type == typeof(EmailAddressAttribute))
64+
else if (typeof(EmailAddressAttribute).IsAssignableFrom(type))
6565
{
6666
adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-email", stringLocalizer);
6767
}
68-
else if (type == typeof(PhoneAttribute))
68+
else if (typeof(PhoneAttribute).IsAssignableFrom(type))
6969
{
7070
adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-phone", stringLocalizer);
7171
}
72-
else if (type == typeof(UrlAttribute))
72+
else if (typeof(UrlAttribute).IsAssignableFrom(type))
7373
{
7474
adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-url", stringLocalizer);
7575
}
76-
else if (type == typeof(FileExtensionsAttribute))
76+
else if (typeof(FileExtensionsAttribute).IsAssignableFrom(type))
7777
{
7878
adapter = new FileExtensionsAttributeAdapter((FileExtensionsAttribute)attribute, stringLocalizer);
7979
}

src/Mvc/Mvc.DataAnnotations/test/ValidationAttributeAdapterProviderTest.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public static TheoryData<ValidationAttribute, Type> DataAnnotationAdapters
4141
{
4242
new RequiredAttribute(),
4343
typeof(RequiredAttributeAdapter)
44+
},
45+
{
46+
new CustomRegularExpressionAttribute("abc"),
47+
typeof(RegularExpressionAttributeAdapter)
4448
}
4549
};
4650
}
@@ -85,5 +89,13 @@ public void AdapterFactory_RegistersAdapters_ForDataTypeAttributes(
8589
var dataTypeAdapter = Assert.IsType<DataTypeAttributeAdapter>(adapter);
8690
Assert.Equal(expectedRuleName, dataTypeAdapter.RuleName);
8791
}
92+
93+
class CustomRegularExpressionAttribute : RegularExpressionAttribute
94+
{
95+
public CustomRegularExpressionAttribute(string pattern) : base(pattern)
96+
{
97+
ErrorMessage = "Not valid.";
98+
}
99+
}
88100
}
89101
}

0 commit comments

Comments
 (0)