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.

3
  • I found some information that might help you here - stackoverflow.com/questions/5395040/… Commented Feb 8, 2013 at 16:35
  • There is a possible solution here: stackoverflow.com/questions/12099466/… Commented Feb 8, 2013 at 16:35
  • Try disabling scripting in browser and see if it works as expected - this could be jQuery validate that overrides your string. Commented Feb 9, 2013 at 0:32

4 Answers 4

2

I found it easier to just add this data annotation:

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

It works client side

Sign up to request clarification or add additional context in comments.

Comments

0

You could try with the Range attribute:

[Range(float.MinValue, float.MaxValue, ErrorMessageResourceName = "IncorrectFloat", ErrorMessageResourceType=typeof(Resources))]
public float MyFloat { get; set; }

Comments

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.

1 Comment

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
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.