I've a fully localized website, which is in mostly in french/english/german.
For now, all was going fine, but I did notice a problem with some error message of asp.net MVC.
I've one property in my model:
[Required]
[LocalizedDisplayName("PublicationDate", NameResourceType = typeof(LocalizationResources.Views.Composer.BaseInfoForm))]
public DateTime PublicationDate { get; set; }
LocalizedDisplayname is an extension of the DisplayNameAttribute, which goes in the Resx file to get the correct translation
The thread CurrentCulture and the CurrentCultureUI are in fr-FR, so the message should be displayed in french(like it does with my [Required]
Attribute, display automatically "Le champ Publication est requis".
But in the case of a DateTime, if I enter something which isn't a date, the validator just returns me a "The value 'asdfasdf' is not valid for Publication.
"
So:
Thank you very much
I think these answer your two questions:
- Why MVC returns me sometimes error message in french, and sometimes in english, in the same form(and I precise, it's the default error message)
One notable difference is that the [Required]
attribute performs explicit client-side validation, whereas when your field does not contain a valid DateTime, you get server-side validation through a failure by the default model binder to create a DateTime object from the posted form data. It's quite a different mechanism, which I guess explains the different outcome. It would have been nice if the outcome had been consistent, of course.
- How to replace this message genericly, by indicating a text like "La valeur {0} n'est pas une date valide pour le champ {1}"
There are two ways:
DefaultModelBinder.ResourceClassKey
property. See the answer to this related question for a description of how to achieve this (for MVC 2 but it hasn't changed in MVC 3)A nicer method (I think) is to do client-side validation. This allows you to provide the error message string you wish from your localized resources. Do it by adding a DataType
attribute as per below, assuming you created a resource class called MyLocalizedResources
with a string having key DateTimeFormatValidationMessage
translated in French as "La valeur {0} n'est pas une date valide pour le champ {1}":
[Required]
[DataType(DataType.Date, ErrorMessageResourceType = typeof(MyLocalizedResources), ErrorMessageResourceName = "DateTimeFormatValidationMessage")]
[LocalizedDisplayName("PublicationDate", NameResourceType = typeof(LocalizationResources.Views.Composer.BaseInfoForm))]
public DateTime PublicationDate { get; set; }
ASP.NET MVC framework does couple of implicit validations: one is required validation and the other one is whether the value is valid for the property or not kind of validation and this two happens eventhough we doesn't decorate the properties with data-annotations.
You have to create keys for PropertyValueInvalid
and PropertyValueRequired
in your global resource class.
This thread will help you Globally localize validation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With