How can I use resources from a different assembly to override the default attribute error messages in my MVC5 application?
My website is namespaced: Company.Web
My resources assembly is namespaced: Company.Web.Resources
I can easily localize attribute error messages individually using:
[Required(ErrorMessageResourceName = "PropertyValueRequired", ErrorMessageResourceType = typeof(Company.Web.Resources.Messages))]
However, since our error message is always "Required", I'd simply like to put the [Required]
attribute without having to specify the resource name. I'd also like to override the default data type messages output by MVC which you can't do via an attribute.
The field {0} must be a date.
I'd like to be
Invalid date
I've seen examples where you can put the resource files in App_GlobalResources (with keys PropertyValueRequired
, FieldMustBeDate
, FieldMustBeNumeric
) and setting ClientDataTypeModelValidatorProvider.ResourceClassKey
, but I already have an external resources assembly I want to use.
I've tried using the following in my Global.asax with no luck:
ClientDataTypeModelValidatorProvider.ResourceClassKey = "Company.Web.Resources.Messages"
How can I accomplish this? Any ideas?
UPDATE (Partial Resolution)
I can solve my attribute-based problem simply by creating new validation adapters and using them in lieu of the default:
public class MyRequiredAttributeAdapter : RequiredAttributeAdapter
{
public MyRequiredAttributeAdapter(ModelMetadata metadata, ControllerContext context, RequiredAttribute attribute)
: base(metadata, context, attribute)
{
if (attribute.ErrorMessage.IsNullOrWhitespace()
&& attribute.ErrorMessageResourceName.IsNullOrWhitespace()
&& attribute.ErrorMessageResourceType == null)
{
attribute.ErrorMessageResourceType = typeof (Resources.Validation.Messages);
attribute.ErrorMessageResourceName = "PropertyValueRequired";
}
}
}
Global.asax
DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(RequiredAttribute), typeof(MyRequiredAttributeAdapter));
However, this still leaves me scratching my head on how to override the default data type message for non-null properties such as DateTime and int. Also, I believe there are some I am unable to override because they are internal (DataTypeAttributeAdapter, CompareAttributeAdapter).