Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net MVC: Some un-localized default error message?

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:

  • 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)
  • How to replace this message genericly, by indicating a text like "La valeur {0} n'est pas une date valide pour le champ {1}"

Thank you very much

like image 339
J4N Avatar asked May 22 '12 06:05

J4N


2 Answers

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:

  1. Tell the default model binder what resource string to use for the error message using the 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)
  2. 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; }
    
like image 182
Clafou Avatar answered Sep 28 '22 03:09

Clafou


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

like image 30
VJAI Avatar answered Sep 28 '22 04:09

VJAI