views:

258

answers:

3

Hi peeps,

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: 

This is the current way I handle displaying of error messages although it does not use data annotations which is what you are looking for - and would be very interested if there is a way to do this. I used the below functionality with MVC 1.0 so not sure what additions have been added in MVC 2.0 which may be of more benefit.

First of all the MyViewModel...

public class MyViewModel
{
    public string FirstName { get; set; }

    public bool IsValid
    {
        get { return (GetRuleViolations().Count() == 0); }
    }

    public IEnumerable<RuleViolation> GetRuleViolations()
    {
        if (String.IsNullOrEmpty(FirstName))
            yield return new RuleViolation("FirstName", "First Name is invalid");

        //...add other validation to FirstName or other properties

        yield break;
    }
}

Then in your Controller you do...

public ActionResult MyAction(MyViewModel model)
{
    if (!model.IsValid)
        ModelState.AddModelErrors(i.GetRuleViolations());

    ...
}

I use a helper method on the ModelState to enable this...

public static class ModelStateExtensions
{
    public static void AddModelErrors(this ModelStateDictionary modelState, IEnumerable<RuleViolation> errors)
    {
        foreach (RuleViolation issue in errors)
        {
            modelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
        }
    }
}
David Liddle
A: 

You can write your own attribute:

public class MyRequiredAttribute : ValidationAttribute
{
    MyRequiredAttribute() : base(() => "{0} blah blah blah blaaaaaah")
    {

    }

    public override bool IsValid(object value)
    {
        if (value == null)
        {
            return false;
        }
        string str = value as string;
        if (str != null)
        {
            return (str.Trim().Length != 0);
        }
        return true;
    }
}

This is copy of RequiredAttribute from Reflector with changed error message.

LukLed
+1  A: 
public class GenericRequired: RequiredAttribute
{
    public GenericRequired()
    {
        this.ErrorMessage = "{0} Blah blah"; 
    }
}
ckarbass