如何创建自定义数据注释验证器 [英] How to create Custom Data Annotation Validators

查看:171
本文介绍了如何创建自定义数据注释验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

婉婷创建自定义的数据注解验证。是否有关于如何创建呢?任何有用的指南/样品

Wanting to create custom data annotation validation. Are there any useful guides / samples on how to create them?

第一:结果
StringLength与最小和最大长度。我知道.NET 4中可以做到这一点,但是想做在.NET 3.5一样的,如果可能的话可以定义最小长度只(至少为x个字符),只有最大长度(最大为x个字符),或两者​​兼而有之(在X和Y字)。

Firstly:
StringLength with minimum and maximum length. I'm aware .NET 4 can do this, but want to do the same in .NET 3.5, if possible being able to define minimum length only (at least x chars), maximum length only (up to x chars), or both (between x and y chars).

第二:结果
使用验证模算术 - 如果数字是一个有效的长度,我希望用11模数算法(我已经实现了它在JavaScript中,所以我想这纯粹是一个简单的移植?)

Secondly:
Validation using modulus arithmetic - if the number is a valid length, I wish to validate using the Modulus 11 algorithm (I have already implemented it in JavaScript, so I guess it would just be a simple porting?)

更新:结果
解决第二个问题,只是一个在JavaScript实现复制的情况下,并作出一些调整,所以并不需要一个解决方案。

Update:
Solved second problem, was just a case of copying over the JavaScript implementation and making a few tweaks, so don't need a solution for that.

推荐答案

要创建自定义数据注解验证请按照下列gudelines:

To create a custom data annotation validator follow these gudelines:


  1. 您的类必须从 System.ComponentModel.DataAnnotations.ValidationAttribute 类继承。

  2. 覆盖 BOOL的IsValid(对象的值)方法并实现它里面的验证逻辑。

  1. Your class has to inherit from System.ComponentModel.DataAnnotations.ValidationAttribute class.
  2. Override bool IsValid(object value) method and implement validation logic inside it.

这就是它。

有时候开发检查值不为空/空和返回false。这是一般的不正确的行为,因为这是在要求验证检查,这意味着您的自定义验证只能验证非空的数据,但返回真正否则(见示例)。这会让他们使用的强制性(必填)和非强制性的领域。

Sometimes developers check that value is not null/empty and return false. This is usually incorrect behaviour, because that's on Required validator to check which means that your custom validators should only validate non-null data but return true otherwise (see example). This will make them usable on mandatory (required) and non-mandatory fields.

public class StringLengthRangeAttribute : ValidationAttribute
{
    public int Minimum { get; set; }
    public int Maximum { get; set; }

    public StringLengthRangeAttribute()
    {
        this.Minimum = 0;
        this.Maximum = int.MaxValue;
    }

    public override bool IsValid(object value)
    {
        string strValue = value as string;
        if (!string.IsNullOrEmpty(strValue))
        {
            int len = strValue.Length;
            return len >= this.Minimum && len <= this.Maximum;
        }
        return true;
    }
}

所有的属性可以在属性如你所愿设置它们进行设置。结果
一些例子:

All properties can be set in attribute as you wish to set them.
Some examples:

[Required]
[StringLengthRange(Minimum = 10, ErrorMessage = "Must be >10 characters.")]

[StringLengthRange(Maximum = 20)]

[Required]
[StringLengthRange(Minimum = 10, Maximum = 20)]

在未设置特定属性,它的值设置在构造函数中,所以它总是有一个值。在上面使用的例子我特意添加了要求验证为好,所以它的同步与上述的警告我已经写了。

When a particular property isn't set, its value is set in the constructor, so it always has a value. In above usage examples I deliberately added the Required validator as well, so it's in sync with the above caution I've written.

所以这个验证仍将对其他没有要求你的模型的价值,但是当它是present它验证(在Web表单认为文本字段的工作,这不是必需的,但如果用户在输入一个值,它必须是有效的)。

So this validator will still work on your model value that's not required, but when it's present it validates (think of a text field in a web form, that's not required, but if a user enters a value in, it has to be valid).

这篇关于如何创建自定义数据注释验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆