When I put [Required] attribute on my ViewModel’s property MVC3 automatically generate error messages like:
The Price field is required.
My site’s single language is Russian, so I want to have localized error messages. I can localize field’s name with [Display(Name = "blablabla")], but how can I localize the field is required part?
Update:
I know, that I can change an error message for concrete field by specifying it [Required(ErrorMessage = "blablabla")], is there a way I can change it in one place for all [Required] attributes, so I could use just [Required] without additional parameters, and it took the localized error message from some ressource/config/etc?
I’ve created an alternative solution where you don’t have to use the attributes for the localization. I’ve created custom model/validation meta data providers.
All you need to do is to download my code and do the following in your global.asax:
(the
ResourceStringProvideris my default implementation but it’s easy to create an alternative that reads from XML files or a database)You can read about it here:
http://blog.gauffin.org/2011/09/easy-model-and-validation-localization-in-asp-net-mvc3/
I’m going to release a nuget package as soon as I’m finished with my view localization and my alternative HTML helpers.