Q:

MVC: Override default ValidationMessage

In the world of MVC I have this view model...
public class MyViewModel{

[Required]
public string FirstName{ get; set; }    }

...and this sort of thing in my view...
<%= Html. ValidationSummary("Please correct the errors and try again.") % >
<%= Html. TextBox("FirstName") %>
<%= Html. ValidationMessage("FirstName", "*") %>

My question:  If I submit this form without supplying a name, I get the following message "The FirstName field is required"
OK.   So, I go and change my property to...
[DisplayName("First Name")]
[Required]
public string FirstName{ get; set; }    

..and now get "The First Name field is required"
All good so far. 
So now I want the error message to display "First Name Blah Blah".   How can I override the default message to display DisplayName + " Blah Blah", wihtout annotating all the properties with something like 
[Required(ErrorMessage = "First Name Blah Blah")]

Cheers,
ETFairfax

A:

public class GenericRequired: RequiredAttribute
{
    public GenericRequired()
    {
        this. ErrorMessage = "{0} Blah blah"; 
    }
}

A:

It seems that RequiredAttribute doesn't implement IClientValidatable, so if you override the RequiredAttribute it breaks client side validation. 
So this is what I did and it works.   Hope this helps someone. 
public class CustomRequiredAttribute : RequiredAttribute, IClientValidatable
{
    public CustomRequiredAttribute()
    {
        this. ErrorMessage = "whatever your error message is";
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        yield return new ModelClientValidationRule
        {
            ErrorMessage = this. ErrorMessage,
            ValidationType = "required"
        };
    }
}

A:

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")].