Tuesday, April 20, 2010

System.ComponentModel.DataAnnotations Validation Attributes – Using Error Message Resource Files

Just a quick-tip if you’ve been making use of the wonderful set of model validation attributes available in System.ComponentModel.DataAnnotations.

If you elect to use the ValidationAttribute’s ErrorMessageResourceName and ErrorMessageResourceType properties, make sure the resource file type you point to is marked with a Public access modifier and not the default value of Internal or you will find that all of your validation attributes will be silently ignored (including System.ComponentModel.DisplayNameAttribute, interestingly enough).

[Required(ErrorMessageResourceName = "EmailMissing", ErrorMessageResourceType = typeof(GeneralForms))]
public string Email { get; set; }

No comments: