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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With