I am working on an ASP.NET Core application and I would like to override the default validation error messages for data-annotations, like Required
, MinLength
, MaxLength
, etc. I read the documentation at Globalization and localization in ASP.NET Core, and it seems that it does not cover what I was looking for...
For instance, a validation error message for the Required
attribute can always be the same for any model property. The default text just states: The {0} field is required, whereby the {0}
placeholder will be filled up with the property’s display name.
In my view models, I use the Required
attribute without any named arguments, like this...
class ViewModel
{
[Required, MinLength(10)]
public string RequiredProperty { get; set; }
}
Setting an ErrorMessage
or ErrorMessageResourceName
(and ErrorMessageResourceType
) is unnecessary overhead, in my opinion. I thought I could implement something similar to IDisplayMetadataProvider
allowing me to return error messages for applied attributes, in case the validation has failed. Is this possible?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…