Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localization for "the value {0} is invalid" in case of int overflow

I've read answers about localization of validation errors by specifying DefaultModelBinder.ResourceClassKey, basically it's when entering string values in int field or not a datetime in datetime field.

But when I'm typing "111111111111111111111111111111" for an int field I get System.OverflowException and it looks like "The value '{0}' is invalid.".

Is there a way to localize (translate that message to other languages) that validation error in a way similar to other MVC-validation?

like image 365
Shaddix Avatar asked May 26 '12 11:05

Shaddix


1 Answers

I had the same issue, and I finally managed to find the solution. Yes, that message can be localized, and luckily it's pretty easy when you figure it out.

You have to create a resource file and put it in the App_GlobalResources folder. You can call the file whatever you want, but I usually call it MvcValidationMessages.

Open the resource file and create a string with the name InvalidPropertyValue and write whatever message you want in the value field.

Now, open the Global.asax file and add the following line to the method Application_Start():

System.Web.Mvc.Html.ValidationExtensions.ResourceClassKey = "MvcValidationMessages";  

"MvcValidationMessages" should of course be the correct name of the resource file you just created.

And voíla! That's all there is to it. The message shown will now be your own instead of the default one.

like image 166
René Avatar answered Oct 03 '22 12:10

René