I'm using DataAnnotions in a ASP.NET MVC application for validate my input models. If I want to use resource files for the error messages, then I have to specify these with named parameters, like so:
[Required(
ErrorMessageResourceType = typeof(Validation),
ErrorMessageResourceName = "NameRequired")]
Since I use this in a bunch of files, I thought, it would be much easier (and more readable) if I could use a constructor like this:
[Required(typeof(Validation), "NameRequired")]
If I write my own custom validation attribute I could implement such a "simple constructor":
public class MyCustomValidationAttribute : ValidationAttribute
{
public MyCustomValidationAttribute(Type resourceType, string resourceName)
{
base.ErrorMessageResourceType = resourceType;
base.ErrorMessageResourceName = resourceName;
}
}
Am I missing something here or want us Microsoft's DataAnnotations team just to write some extra lines? :-)
EDIT:
Just for clarification: I have a resource file called "Validation.resx".