Using a resx file in the App_GlobalResources directory, I've been able to change the default message for the PropertyValueInvalid string of the model validators.
But it doesn't work to translate the message when a value is required (PropertyValueRequired.)
In the Global.asax.cs Application_Start() I've changed the resource class key, like this:
DefaultModelBinder.ResourceClassKey = "Messages";
And in the Messages.resx files I've put two entries:
Thanks.
RequiredAttribute not used DefaultModelBinder.GetValueRequiredResource. Create custom DataAnnotationsModelValidator class.
public class MyRequiredAttributeAdapter : RequiredAttributeAdapter
{
public MyRequiredAttributeAdapter(ModelMetadata metadata,
ControllerContext context,
RequiredAttribute attribute)
: base(metadata, context, attribute)
{
attribute.ErrorMessageResourceType = typeof (Messages);
attribute.ErrorMessageResourceName = "PropertyValueRequired";
}
}
and register adapter in Global.asax.
DataAnnotationsModelValidatorProvider.RegisterAdapter(
typeof(RequiredAttribute),
typeof(MyRequiredAttributeAdapter));
Hope this help!
Reusable Validation Error Message Resource Strings for DataAnnotations
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