简体   繁体   English

自定义验证属性有多个实例问题

[英]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 我在C#4中使用tha命名空间System.ComponentModel.DataAnnotations来实现我自己的验证属性,它看起来像这样

[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)? 如何执行所有验证(使用asp.net mvc 2)?

如果你想实现你自己的属性=的AllowMultiple true,那么第一个覆盖TYPEID和明年的解决方案的JQuery看看代码项目的文章在这里

Take a look at FluentValidation . 看看FluentValidation It allows you to separate your validation from the classes being validated so that you can call your validation logic at any time, on the server or the client. 它允许您将验证与要验证的类分开,以便您可以随时在服务器或客户端上调用验证逻辑。

It allows you to add as many rules of any complexity to a class, without cluttering it with attributes. 它允许您为类添加任意数量的复杂规则,而不会使用属性使其混乱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM