Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom validation attribute with multiple instances problem

I'm using tha namespace System.ComponentModel.DataAnnotations in C# 4 to implement my own validation attribute and it looks like this

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public sealed class MyCustomValidator : ValidationAttribute {
    private String Property1 { get; set; }
    private String Property2 { get; set; }

    public ValeTaxiSituacaoRequired(String property1, String property2) {
        Property1 = property1;
        Property2 = property2;
    }

    public override bool IsValid(object value) {
        //validation logic
    }

}

I wanna use this attribute as below

[MyCustomValidator("Name", "Job")]
[MyCustomValidator("Name", "Email")]
[MyCustomValidator("Name", "Job")]
public class Employe {
}

The problem is that just one validation is perfomed. How can I execute all the validations (using asp.net mvc 2)?

like image 700
Murilo Lima Avatar asked Jul 29 '10 13:07

Murilo Lima


2 Answers

you have to override TypeId property http://www.paraesthesia.com/archive/2010/03/02/the-importance-of-typeid-in-asp.net-mvc-dataannotations-validation-attributes.aspx

like image 103
Boies Ioan Avatar answered Oct 03 '22 02:10

Boies Ioan


If you want to implement AllowMultiple=true on your own attribute then first override TypeID and next for solution to JQuery look at the article on code project here

like image 39
Satish Avatar answered Oct 03 '22 02:10

Satish