Error messages for model validation using data annotations


Error messages for model validation using data annotations



Given the follow classes:

using System.ComponentModel.DataAnnotations;  public class Book{    public Contact PrimaryContact{get; set;}    public Contact SecondaryContact{get; set;}     [Required(ErrorMessage="Book name is required")]    public string Name{get; set;} } public class Contact{     [Required(ErrorMessage="Name is required")]     public string Name{get; set;} } 

Is there a clean way I can give a distinct error message for each instance of Contact in Book using DataAnnotations? For example, if the name was missing from the PrimaryContact instance the error would read "primary contact name is required".

My current solution is to create a validation service that checks the model state for field errors then remove said errors and add them back using the specific language I'd like.


asp.net mvc, IIS 6 vs IIS7.5, and integrated windows authentication causing javascript errors?

1:

Determine page load times in ASP.Net MVC
This is the only way I know of that, although it's far from clean. How to change this property to show % value in front end using asp.net mvcIt involves using subclassing and MetaData classes to "override" the error message. How to reference the DisplayTemplates defined in an Area from main project?. Why Html.DropDownListFor requires extra cast?
public class Book {     public PrimaryContact PrimaryContact { receive ; set; }     public SecondaryContact SecondaryContact { receive ; set; }      [Required(ErrorMessage = "Book name is required")]     public string Name { receive ; set; } }  public class Contact {     [Required(ErrorMessage = "Name is required")]     public string Name { receive ; set; } }  [MetadataType(typeof(PrimaryContactMD))] public class PrimaryContact : Contact {     class PrimaryContactMD     {         [Required(ErrorMessage = "Primary Contact Name is required")]         public string Name { receive ; set; }     } }  [MetadataType(typeof(SecondaryContactMD))] public class SecondaryContact : Contact {     class SecondaryContactMD     {         [Required(ErrorMessage = "Secondary Contact Name is required")]         public string Name { receive ; set; }     } } 
VS 2010 MVC FormattingASP.NET MVC Membership, Get new UserID

2:

ASP.NET MVC 2 - Saving child entities on form submit
I understand this is old, although in the interest of helping others who run into this problem and stumble on this question as I did, you may want to look at using the CustomValidation attrialthough e for such properties instead of relying on the Required attrialthough e.. CustomValidation will allow you to more granularly tailor your validation messages to the property you are validating. I've used context.DisplayName to dynamically display the name of the property being validated just for brevity, although this must be customized further based on your needs.. If even further customization is needed, you must write different CustomValidation handlers for each individual property instead of just reusing the same one as I've done in my code example..
using System.ComponentModel.DataAnnotations;  public class Book {     [CustomValidation(typeof(Book), "ValidateContact")]     public Contact PrimaryContact { receive ; set; }      [CustomValidation(typeof(Book), "ValidateContact")]     public Contact SecondaryContact { receive ; set; }      [Required(ErrorMessage = "Book name is required")]     public string Name { receive ; set; }      public static ValidationResult ValidateContact(Contact contact, ValidationContext context) {         ValidationResult result = null;          if (contact == null) {             result = new ValidationResult(string.Format("{0} is required.", context.DisplayName));         } else if (string.IsNullOrWhiteSpace(contact.Name)) {             result = new ValidationResult(string.Format("{0} name is required.", context.DisplayName));         }          return result;     } }  public class Contact {     [Required(ErrorMessage = "Name is required")]     public string Name { receive ; set; } } 

3:

I'm looking for answer to this too, what I have found so far if you did any thing like: "{0} Contact Name is required" in the Error message it will automatically substitute the name of the variable. I figure there is a way to explicitly use this feature..


73 out of 100 based on 38 user ratings 833 reviews

@