In the world of MVC I have this view model...
public class MyViewModel{
[Required]
public string FirstName{ get; set; } }
...and this s
Here is a way to do it without subclassing RequiredAttribute
. Just make some attribute adapter classes. Here I'm using ErrorMessageResourceType
/ErrorMessageResourceName
(with a resource) but you could easily set ErrorMessage
, or even check for the existence of overrides before setting these.
Global.asax.cs:
public class MvcApplication : HttpApplication {
protected void Application_Start() {
// other stuff here ...
DataAnnotationsModelValidatorProvider.RegisterAdapter(
typeof(RequiredAttribute), typeof(CustomRequiredAttributeAdapter));
DataAnnotationsModelValidatorProvider.RegisterAdapter(
typeof(StringLengthAttribute), typeof(CustomStringLengthAttributeAdapter));
}
}
private class CustomRequiredAttributeAdapter : RequiredAttributeAdapter {
public CustomRequiredAttributeAdapter(ModelMetadata metadata, ControllerContext context, RequiredAttribute attribute)
: base(metadata, context, attribute)
{
attribute.ErrorMessageResourceType = typeof(Resources);
attribute.ErrorMessageResourceName = "ValRequired";
}
}
private class CustomStringLengthAttributeAdapter : StringLengthAttributeAdapter {
public CustomStringLengthAttributeAdapter(ModelMetadata metadata, ControllerContext context, StringLengthAttribute attribute)
: base(metadata, context, attribute)
{
attribute.ErrorMessageResourceType = typeof(Resources);
attribute.ErrorMessageResourceName = "ValStringLength";
}
}
This example would have you create a resource file named Resources.resx with ValRequired
as the new required default message and ValStringLength
as the string length exceeded default message. Note that for both, {0}
receives the name of the field, which you can set with [Display(Name = "Field Name")]
.
I was trying to solve the same thing and I found very simple solution in MVC 4.
First, you would probably have to store error messages in some place. I used custom resources, well described at http://afana.me/post/aspnet-mvc-internationalization.aspx.
Important is, that I can get any resource (even error message) by simply calling ResourcesProject.Resources.SomeCustomError or ResourcesProject.Resources.MainPageTitle etc. Everytime I try to access Resources class, it takes culture info from current thread and returns right language.
I have error message for field validation in ResourcesProject.Resources.RequiredAttribute. To set this message to appear in View, simply update [Required] attribute with these two parameters.
ErrorMessageResourceType - Type which will be called (in our example ResourcesProject.Resources)
ErrorMessageResourceName - Property, which will be called on the type above (In our case RequiredAttribute)
Here is a very simplified login model, which shows only username validation message. When the field is empty, it will take the string from ResourcesProject.Resources.RequiredAttribute and display this as an error.
public class LoginModel
{
[Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName="RequiredAttribute")]
[Display(Name = "User name")]
public string UserName { get; set; }
}