0

I can localize most of data annotations in asp.net mvc. But, for some of them I can't. For example look at this one:

public class TestModel {
    [Required(ErrorMessageResourceName="MyFloat",ErrorMessageResourceType=typeof(MyResource))]
    [Display(Name = "MyFloat", ResourceType = typeof(MyResource))]
    public float MyFloat { get; set; }
}

As you can see, I can localize Required and Display attributes. But, if end-user type a string in field, the validator throws an error with this message:

The field MyFloat must be a number.

I searched all attributes to find an attribute to change this message, but I couldn't. Have you any idea please? Thanks in advance.

Display Name
  • 4,672
  • 1
  • 33
  • 43
amiry jd
  • 27,021
  • 30
  • 116
  • 215
  • I found some information that might help you here - http://stackoverflow.com/questions/5395040/globally-localize-validation/5395297#5395297 – Trax72 Feb 08 '13 at 16:35
  • There is a possible solution here: http://stackoverflow.com/questions/12099466/the-field-must-be-a-number-how-can-change-this-message-to-another-lenguage – stralsi Feb 08 '13 at 16:35
  • Try disabling scripting in browser and see if it works as expected - this could be jQuery validate that overrides your string. – OakNinja Feb 09 '13 at 00:32

4 Answers4

2

I found it easier to just add this data annotation:

 [RegularExpression(@"^[0-9]+$", ErrorMessageResourceName="MyFloat",ErrorMessageResourceType=typeof(MyResource))]

It works client side

Nick
  • 2,877
  • 2
  • 33
  • 62
0

You could try with the Range attribute:

[Range(float.MinValue, float.MaxValue, ErrorMessageResourceName = "IncorrectFloat", ErrorMessageResourceType=typeof(Resources))]
public float MyFloat { get; set; }
Olivier Payen
  • 15,198
  • 7
  • 41
  • 70
0

This is a question of sequence of events. Framework's vs your custom ones.

The default message you receiving is built deeply into the framework. It is a string resource. Default model binder is adding it when binding string value to double type. This is obviously done before your custom validation is and hence you getting this default error message first.

To change this behavior you might want write a custom model binder. Example of how to create custom model binder is here.

Hope this helps.

Community
  • 1
  • 1
Display Name
  • 4,672
  • 1
  • 33
  • 43
0

If it's client-side you're worried about just add this either to the end of the jquery.validate.js file or on a separate javascript file and reference it right after:

jQuery.extend(jQuery.validator.messages, {
  required: "Este campo es obligatorio.",
  remote: "Por favor, llenar este campo.",
  email: "Debe escribir una dirección de correo válida",
  url: "Debe escribir una dirección válida.",
  date: "Debe escribit una fecha válida.",
  dateISO: "Debe escribit una fecha (ISO) válida.",
  number: "Debe escribir un número válido.",
  digits: "Por favor, esribir sólo dígitos.",
  creditcard: "Debe escribir un número de tarjeta válido.",
  equalTo: "Por favor, escribir el mismo valor nuevamente.",
  accept: "Por favor, escribir un valor con una extensión aceptada.",
  maxlength: jQuery.validator.format("Por favor, no escribir más de {0} caracteres."),
  minlength: jQuery.validator.format("Por favor, no escribir menos de {0} caracteres."),
  rangelength: jQuery.validator.format("Por favor, escribir un valor entre {0} y {1} caracteres."),
  range: jQuery.validator.format("Por favor, escribir un valor entre {0} y {1}."),
  max: jQuery.validator.format("Por favor, escribir un valor menor o igual a {0}."),
  min: jQuery.validator.format("Por favor, escribir un valor mayor o igual a {0}.")
});

That should localize your messages.

amhed
  • 3,649
  • 2
  • 31
  • 56
  • Note: this is the easiest way to hard code the jquery.validate localizations. For projects with multiple possible localizations I have a folder with all the localized .js files (validate-es.js, validate-us.js, etc) and decide which one to render from the BundleConfig method and the configured locale on the web.config – amhed Feb 14 '13 at 05:12