ASP.NET MVC 2: Data DataAnnotations validation be convention

Posted by stacker on Stack Overflow See other posts from Stack Overflow or by stacker
Published on 2010-05-28T20:13:33Z Indexed on 2010/05/28 20:32 UTC
Read the original article Hit count: 241

I have a required attribute that used with resources:

public class ArticleInput : InputBase
{
    [Required(ErrorMessageResourceType = typeof(ArticleResources), ErrorMessageResourceName = "Body_Validation_Required")]
    public string Body { get; set; }
}

I want to specify the resources be convention, like this:

public class ArticleInput : InputBase
{
    [Required2]
    public string Body { get; set; }
}

Basically, Required2 implements the values based on this data:

ErrorMessageResourceType = typeof(ClassNameWithoutInput + Resources); // ArticleResources
ErrorMessageResourceName = typeof(PropertyName + "_Validation_Required"); // Body_Validation_Required

Is there any way to achieve something like this? maybe I need to implement a new ValidationAttribute.

© Stack Overflow or respective owner

Related posts about asp.net-mvc-2

Related posts about dataannotations