1

I have an MVC Model class that looks like this:

[ValidateNewRegistration]
public class NewRegistrationModel {

    [System.ComponentModel.DisplayName("Profile Display Name")]
    [Required(ErrorMessage = "Display Name must be specified.")]
    [StringLength(50)]
    public string DisplayName { get; set; }

    [System.ComponentModel.DisplayName("Email Address")]
    [Required(ErrorMessage = "Email address must be specified.")]
    [RegularExpression("^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$",
              ErrorMessage = "The email address you specified is invalid.")]
    [StringLength(100)]
    public string EmailAddress1 { get; set; }

}

In my view, I have this:

    @Html.ValidationSummary()
    @Html.EditorForModel()

I understand how to use the metadata to mark up the attributes, but I want to do is implement my own Editor Template that has two fields in it ... a first name and last name, that will be used in at least 1/2 dozen places in my website.

How do I add error messages so that they appear in the Validation Summary, but in a way that is contained within the EditorTemplate?

Long term what I am looking for is the ability to put a "[UIHint("--Editor Template--")]" into the classes, and have the editor template self-contained enough to issuing it's onw error message for the form field. So that I do not have to add metadata to the class that says [Required...] or [RegularExpression...]. My end goal is a class that looks like this:

[ValidateNewRegistration]
public class NewRegistrationModel {

    [System.ComponentModel.DisplayName("Profile Display Name")]
    [UIHint("DisplayName")]
    public string DisplayName { get; set; }

    [System.ComponentModel.DisplayName("Email Address")]
    [UIHint("EmailAddress")]
    public string EmailAddress1 { get; set; }

    [System.ComponentModel.DisplayName("Email Address (confirm)")]
    [UIHint("EmailAddress")]
    public string EmailAddress2 { get; set; }

}
Elenesski
  • 409
  • 1
  • 5
  • 17

1 Answers1

0

A similar question has already been asked here: Is there a way to reuse data annotations?

Check the solutions there and comment if you need help with anything specific

    class UIHintAttribute : ValidationAttribute
{
    public string Field { get; set; }

    public UIHintAttribute(string field)
    {
        Field = field;
    }
    public override bool IsValid(object value)
    {

        if (Field == "DisplayName")
        {
            if (new RequiredAttribute { ErrorMessage = "DisplayName cannot be blank." }.IsValid(value) && new StringLengthAttribute(50).IsValid(value))
                return true;

            return false;
        }
        if (Field == "EmailAddress")
        {
            if (new RequiredAttribute { ErrorMessage = "Email address cannot be blank." }.IsValid(value)
                && new RegularExpressionAttribute("^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$") { ErrorMessage = "The email address you specified is invalid." }.IsValid(value)
                    && new StringLengthAttribute(100).IsValid(value))
                return true;

            return false;
        }

        // Needs to return true by default unless Required exists
        return true;
    }
}

Now you should be able to use it the way you wanted to.

Edit:

Long term what I am looking for is the ability to put a "[UIHint("--Editor Template--")]" into the classes, and have the editor template self-contained enough to issuing it's onw error message for the form field. So that I do not have to add metadata to the class that says [Required...] or [RegularExpression...].

The class above will make the following possible:

public class NewRegistrationModel {

    [System.ComponentModel.DisplayName("Profile Display Name")]
    [UIHint("DisplayName")]
    public string DisplayName { get; set; }

    [System.ComponentModel.DisplayName("Email Address")]
    [UIHint("EmailAddress")]
    public string EmailAddress1 { get; set; }

    [System.ComponentModel.DisplayName("Email Address (confirm)")]
    [UIHint("EmailAddress")]
    public string EmailAddress2 { get; set; }

}
Community
  • 1
  • 1
galdin
  • 12,411
  • 7
  • 56
  • 71
  • This isn't an answer to the question. My question is, how to I create an EditorTemplate with custom errors? – Elenesski Nov 05 '13 at 06:57
  • Your code doesn't actually work. The framework will look for it's own version of the UIHint attribute, not an override of it. However, ValidationAttribute is the way to go. I'd use UIHint when the system didn't pick the right editor, and [EmailAddressValidator] to put in the validation logic for email addresses. That at least centralizes the validation logic at a property level. – Elenesski Nov 05 '13 at 17:28
  • @Elenesski I've corrected the typos in the `UIHintAttribute` class, please check against the updated code If you still find it not working, please give me a description of what's wrong so I can help you, and it would be great if you could explain `ValidateNewRegistrationAttribute` as well – galdin Nov 05 '13 at 21:37