Reputation: 13958
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 :-(
Upvotes: 1
Views: 894
Reputation: 13058
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.
Upvotes: 1