1

I'm working on an ASP.Net MVC 3 project, which needs localized error messages from its validation rules.

Is there any way to do this client side ?

I can live with english serverside validation messages, as the client side ones should always be triggered (I've no fear my users will disable javascript, as it's an Intranet site)

Whilst I can use DataAnnotations for stuff such as [Required] and [StringLength], there is no such option (it seems) for data type validation.

If for instance I have int Portions in my view model, it's automatically validate input to ensure it's an int. Fair enough, but the validation message is always in english :-(

Suppose my model looks like this:

public class RecipeModel
{

[Display(Name = "Navn")]
[Required(ErrorMessage = "Skal udfyldes")]
public string Name { get; set; }

[Display(Name = "Portioner")]
public int Portions { get; set; }

}

As you can see I have a custom errormessage for "Name", however if I try to enter text into the EditorFor(Portions), it'll show my this errormessage:

"The field Portioner must be a number."

And that's the errormessage I want to localize. Unfortunately there's no DataAnnotation for that built-in validator (checking that it's an int).

I even tried using a DataType annotation of type Custom, to no avail :-(

1
  • How are you validating? With asp validators? If so you should be able to amend the validator with the localised message when rendering the page. Commented Jun 30, 2011 at 14:41

1 Answer 1

1

You can create your own validation messages using data annotations with your model. If you want to support multiple languages you can create a resource file for each language and link from the data annotation to the Id of the message in your resource file.

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

5 Comments

Oh sorry I forgot to mention what kind of validation troubles me. I'm updating the question.
You can change the default English validation messages. Look at the "ErrorMessage" parameter in the Required attribute.
Yes i realize that, it's how to chance the validation message for data typen, such as int, dates and so forth. Sorry if my question was too unclear :-/
All the standard validation attributes have an ErrorMessage that can be customized.
Yes, unfortunately the datatype validation happens solely based on the actual datatype (int, DateTime, etc.) - that is, without any data annotations. I'll add some code to the question, hopefully that'll clarify it better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.