ASP.NET MVC Custom Validation Summary for Address Fields

I am trying to find the best way to check one page validation. It contains:

  • ship address
  • billing address
  • and etc.

The class class obviously contains First Name , Last Name , Street1 , Street2 , City , State , Zip , Phone , etc.

Let's say that the user clicks "OK" before entering anything - then you will get a dozen or more verification errors, giving you a large block of red text that looks just ugly.

I would like to check the address as a single object and give an intellectual error - for example, an "incomplete address" or more specific errors, if necessary. But I still want to be able to highlight every single area that has problems. I don’t see an easy way to do this right now, because obviously the Html.ValidationSummary will show each field.

So, I want to show the summary as:

  "Your shipping address is incomplete" 

and highlight Zip and City red.

I think I would need to execute a fully custom ValidationSummary file and possibly even a fully custom data structure.

Make all validation frameworks easier to make this summary when the summary should display an intelligent summary, and not just every single field error.


Edit: MVC 2 RC now supports model-level errors.

ValidationSummary now supports overloads where only model level errors are displayed. This is useful if you show the validation messages located next to each form field. Previously, these messages will be duplicated in the resume validation. With these new changes, you can have a summary display of a general verification message (for example, "There were errors in the presentation of your form") as well as a list of validation messages that do not apply to a specific field.

Has anyone got a real sample of how to do this?

+4
source share
5 answers

You can use the composite property Address and check the entire address as a unit:

 public class Address { public string Street { get; set; } public string City { get; set; } public string Zip { get; set; } } public class Order { [Required] public string FirstName { get; set; } [Required] public string LastName { get; set; } [AddressRequired("Your shipping address is incomplete")] public Address ShipTo { get; set; } [AddressRequired("Your billing address is incomplete")] public Address BillTo { get; set; } // you could do this if you still need 1:1 mapping for model binding public string ShippingCity { get { return ShipTo.City; } set { ShipTo.City = value; } } } 

And the validation attribute will look something like this:

 public class AddressRequiredAttribute : ValidationAttribute { ... public override bool IsValid(object value) { var address = value as Address; if (address != null) { ... } } } 
+3
source

IDataErrorInfo has two members:

  • Error. Gets an error message indicating what is wrong with this object.
  • Item - returns an error message for a property with the specified name.

If you implement the Error element, you will have one error message.

+1
source

I am looking at a similar problem in a recent project, I made a custom validation summary, here is the code:

 <% if (!ViewData.ModelState.IsValid) { Response.Write("<div class=\"prepend-1 span-10 last notice\">"); Response.Write("<span>Please fix fields marked with an asteristk <span class=\"ss_sprite ss_asterisk_orange\"> </span></span>"); Response.Write("<ul>"); foreach (KeyValuePair<string, ModelState> keyValuePair in ViewData.ModelState) { foreach (ModelError modelError in keyValuePair.Value.Errors) { %> <li><%= Html.Encode(modelError.ErrorMessage)%></li> <% } } Response.Write("</ul>"); Response.Write("</div>"); } %> 

I did this in a partial view, but it might be better to wrap this in the HTML Helper method, as in the original ValidationSummary.

Inside, you can check out any special and unique requirements. Hope this helps.

+1
source

Here is what I would do:

Put your validation errors in ModelState, whichever way you prefer. You can add them directly to ModelState in your controller using IDataErrorInfo or with DataAnnotations and a validation runner. It doesn’t matter if you populate ModelState with your errors and redisplay the view.

Then, make sure that all of your inputs also have the corresponding Html.ValidationMessage () associated with them in your form:

 <%= Html.TextBox("city") %> <%= Html.ValidationMessage("city", "*") %> 

Depending on your css rules for validation error classes, this will turn the text box into red and display a red asterisk next to it telling the user that they need to correct their input.

Finally, since you are not interested in displaying a full check summary, just do a simple check to check if the ModelState is valid and if it does not display your general message.

 <% if (!ViewData.ModelState.IsValid) { %> <div id="validation-message">Your Shipping Address in Incomplete</div> <% } %> 

This solution will highlight specific fields that the user has incorrectly filled out and display a brief description of the errors as you want.

+1
source
Scott has just posted a great blog post about new validation features .

While he does not delve into the question of how to implement model-level validation, he points to the default ASP.NET MVC application project template as an explanation of how to do this:

In addition to the creation of test attributes that are applicable to the individual properties of the object, you can also apply at the class level verification attribute that allows you to perform validation logic in a few properties within the object. For an example of this in action, you can consider the “PropertiesMustMatchAttribute” attribute, which is included in the AccountModels.cs / vb file in the ASP.NET MVC 2 application by default project template (just make File-> New ASP.NET MVC 2 Web Project in VS 2010 and look for this class).

0
source

All Articles