-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add localizers to validation attributes #3200
Conversation
@@ -19,16 +22,26 @@ public class DataAnnotationsClientModelValidatorProvider : IClientModelValidator | |||
{ | |||
// A factory for validators based on ValidationAttribute. | |||
internal delegate IClientModelValidator | |||
DataAnnotationsClientModelValidationFactory(ValidationAttribute attribute); | |||
DataAnnotationsClientModelValidationFactory(ValidationAttribute attribute, IStringLocalizer stringLocalizer); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this would be much easier to read if you put the line break after DataAnnotationsClientModelValidationFactory(
@NTaylorMullen, @rynowak : updated |
DataAnnotationsClientModelValidationFactory(ValidationAttribute attribute); | ||
DataAnnotationsClientModelValidationFactory( | ||
ValidationAttribute attribute, | ||
IStringLocalizer stringLocalizer); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this would be much easier to read if you put the line break after
DataAnnotationsClientModelValidationFactory(
And now DataAnnotationsClientModelValidationFactory(
can move up to the previous line and it looks like normal code!
_stringLocalizerFactory != null) | ||
{ | ||
stringLocalizer = _options.Value.DataAnnotationLocalizerProvider( | ||
context.ModelMetadata.ContainerType ?? context.ModelMetadata.ModelType, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious: Why do we prefer the ContainerType
here as opposed to the model type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Primitive data types like string
, int
are also treated as ModelType
. We don't want to create .resx
for each data types/property. ContainerType
is the Complex type. So we want to look at ModelType
only if ContainerType
is null
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That doesn't have anything to do with what kind of type it is.
Validators are either retrieved for a property or a type. If you're looking at a property, then you'll have a container type.
⌚ Also, are the travis failures due to missing changes from Localization? |
@rynowak, @NTaylorMullen updated |
|
||
private readonly Dictionary<Type, DataAnnotationsClientModelValidationFactory> _attributeFactories = | ||
BuildAttributeFactoriesDictionary(); | ||
private IOptions<MvcDataAnnotationsLocalizationOptions> _options; | ||
private IStringLocalizerFactory _stringLocalizerFactory; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These can both be made readonly
|
|
Thanks @rynowak and @NTaylorMullen. Merged 0889b18 |
cc @rynowak, @NTaylorMullen