views:

31

answers:

1

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".

A: 

I hear you and feel your pain. We have a database with thousands of items that need data annotations.

One option is to use resource files. It may seem like even more work at first, but you can reuse resources for simple things like "Name required". See this StackOverflow item for some leads.

Cylon Cat
I'm using resource files... And it works great, but I'm asking myself, if there couldn't be a simpler constructor for the validation attributes.
Dave
As written, I don't think so.
Cylon Cat