每个验证属性的所有不显眼的验证属性列表

List of all unobtrusive validation attributes for each Validation Attribute

我需要每个验证属性的所有不显眼的验证属性的参考列表。类似于:

MVC 为它提供的每个数据注释验证器提供了不显眼的验证器。摘自 Validation with Data Annotation Validators,这是该列表:

Using the Data Annotation Validator Attributes

When you use the Data Annotations Model Binder, you use validator attributes to perform validation. The System.ComponentModel.DataAnnotations namespace includes the following validator attributes:

  • Range – Enables you to validate whether the value of a property falls between a specified range of values.
  • ReqularExpression – Enables you to validate whether the value of a property matches a specified regular expression pattern.
  • Required – Enables you to mark a property as required.
  • StringLength – Enables you to specify a maximum length for a string property.
  • Validation – The base class for all validator attributes.
  • DataType - Additional validations for specific data types, like phone numbers, credit cards and email addresses. Not in the referenced link.

另请参阅 https://dataannotationsextensions.apphb.com 以了解可包含在您的应用程序中的其他验证器。

就客户端标记属性而言,这些属性由上述注释生成的不显眼的适配器处理。这些以“data-val-”为前缀。验证器的附加参数将作为附加属性添加。例如: regex 变为 data-val-regex="Message" data-val-regex-pattern="some pattern"

来自 MVC3 jQuery.validate.unobtrusive.js:

adapters.addSingleVal("accept", "exts")
        .addSingleVal("regex", "pattern");

adapters.addBool("creditcard")
        .addBool("date")
        .addBool("digits")
        .addBool("email")
        .addBool("number")
        .addBool("url");

adapters.addMinMax("length", "minlength", "maxlength", "rangelength")
        .addMinMax("range", "min", "max", "range");

adapters.add("equalto", ["other"], function (options) {
        // removed for brevity
});
adapters.add("required", function (options) {
    // removed for brevity
});
adapters.add("remote", ["url", "type", "additionalfields"], function (options) {
    // removed for brevity
});

这是一个老问题,虽然有答案,但仍然缺乏简单的 table 描述 C# 中的不同属性和匹配的数据属性。以下是包含此信息的 table。

Table

备注

  • 所有数据验证属性都将创建 data-val-<name>="<message>" 形式的数据属性,任何属性都将采用 data-val-<name>-<param>="<value>"
  • 形式
  • 在table中,HTML Name列表示数据属性中的名称。例如。 data-val-maxlength
  • 在table中Parameter Name列表示验证名称后数据属性中的参数名称。例如。 data-val-maxlength-max
  • Message Index列是ErrorMessage中参数的索引,索引0始终是字段名。例如。 ErrorMessage = "The field {0} cannot be longer than {1}"
  • 此数据取自 .NET Core 存储库,可能还有其他库提供的其他属性

参考资料