I can confirm that this is working in ASP.NET MVC 2. You *have* to use GlobalResources though -- it does not work with custom resource files, since the DefaultModelBinder uses HttpContext.GetGlobalResourceObject to obtain ResourceManager (which looks into
App_GlobalResources to find the resources...)
Miha
Marked as answer by ricka6 on Apr 30, 2010 05:12 PM
If I run this method HttpContext.GetGlobalResourceObject("OverridedMessages", "PropertyValueInvalid") The return is "O valor '{0}' não é válido para {1}." PT culture or "The value '{0}' is not valid for {1}." En culture.
So i guess i am configuring this rigth.
My problem is that the default message when I input letters to a number field is "The field
XYZ must be a number." wich is different from the "The value '{0}' is not valid for {1}."
I ended up doing a custom validation attribute to assure that the field is a number (positive number, in my case) and now the error messages that it displays is my custom validation message.
That message is part of MVC and provided by the ClientDataTypeModelValidatorProvider. Aside from the standard localization of MVC, this message isn't easily changeable (it was added at the last minute, and we didn't offer an override unfortunately). You
can remove this behavior entirely (data type client-side validation) by removing that validator provider; you could also write your own, which returned "number" client-side validation rules with whatever message you wanted. The easiest way to do that would
be to start with the source for ClientDataTypeModelValidatorProvider and making whatever modifications you feel necessary.
Note that this validator provider is a client-side only validator provider, intended to verify that you only enter digits (and commas/decimal points) for values which are inherently numeric, like int, double, or decimal. Server side validation occurs during
model binding automatically when the system attempts to convert your value into the correct type.
Marked as answer by ricka6 on Apr 30, 2010 05:12 PM
One year later :) Has there been any progress on this? in MVC3 space? I'm running into a similar problem where I want to change the default error messages and sync them up so atleast server and client validation show the same error message. Both are
generated on the server so I don't see why not. The [Required] attribute does it right.
That message is part of MVC and provided by the ClientDataTypeModelValidatorProvider. Aside from the standard localization of MVC, this message isn't easily changeable (it was added at the last minute, and we didn't offer an override unfortunately).
This is a real pain if you're in a non english culture. I would vote for a better support for localization in ASP.Net MVC 4. Ideally the framework should provide localized messages according to the thread culture. If not, at least there should be a centralized
place where we could override ALL client and server validation messages with our own.
mihav
Member
15 Points
16 Posts
Re: Change default validation messages
Apr 28, 2010 11:37 AM|LINK
Diego,
I can confirm that this is working in ASP.NET MVC 2. You *have* to use GlobalResources though -- it does not work with custom resource files, since the DefaultModelBinder uses HttpContext.GetGlobalResourceObject to obtain ResourceManager (which looks into App_GlobalResources to find the resources...)
Miha
diegosps
Member
30 Points
24 Posts
Re: Change default validation messages
Apr 29, 2010 06:18 PM|LINK
Hi Miha.
If I run this method HttpContext.GetGlobalResourceObject("OverridedMessages", "PropertyValueInvalid") The return is "O valor '{0}' não é válido para {1}." PT culture or "The value '{0}' is not valid for {1}." En culture.
So i guess i am configuring this rigth.
My problem is that the default message when I input letters to a number field is "The field XYZ must be a number." wich is different from the "The value '{0}' is not valid for {1}."
I ended up doing a custom validation attribute to assure that the field is a number (positive number, in my case) and now the error messages that it displays is my custom validation message.
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: Change default validation messages
Apr 29, 2010 10:13 PM|LINK
That message is part of MVC and provided by the ClientDataTypeModelValidatorProvider. Aside from the standard localization of MVC, this message isn't easily changeable (it was added at the last minute, and we didn't offer an override unfortunately). You can remove this behavior entirely (data type client-side validation) by removing that validator provider; you could also write your own, which returned "number" client-side validation rules with whatever message you wanted. The easiest way to do that would be to start with the source for ClientDataTypeModelValidatorProvider and making whatever modifications you feel necessary.
Note that this validator provider is a client-side only validator provider, intended to verify that you only enter digits (and commas/decimal points) for values which are inherently numeric, like int, double, or decimal. Server side validation occurs during model binding automatically when the system attempts to convert your value into the correct type.
TvdH
Member
2 Points
1 Post
Re: Change default validation messages
Apr 27, 2011 12:41 AM|LINK
One year later :) Has there been any progress on this? in MVC3 space? I'm running into a similar problem where I want to change the default error messages and sync them up so atleast server and client validation show the same error message. Both are generated on the server so I don't see why not. The [Required] attribute does it right.
agrinei
Member
52 Points
11 Posts
Re: Change default validation messages
May 24, 2011 12:54 AM|LINK