Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of all unobtrusive validation attributes for each Validation Attribute

I need a reference list of all unobtrusive validation attributes for each Validation Attribute. Something like:

enter image description here

like image 355
Ala Avatar asked Sep 02 '15 15:09

Ala


2 Answers

MVC ships with unobtrusive validators for each of the Data Annotation Validators that it provides. Taken from Validation with Data Annotation Validators, here is that list:

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.

See also https://dataannotationsextensions.apphb.com for additional validators that can be included in your application.

As far as client side tag attributes are concerned, these are processed by the unobtrusive adapters that the above annotations generate. These are prefixed with "data-val-". Additional parameters for the validator would be added as additional attributes. For example: regex becomes data-val-regex="Message" data-val-regex-pattern="some pattern"

From 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
});
like image 60
B2K Avatar answered Oct 21 '22 18:10

B2K


This is an old question and although there is an answer there was still a lack of a simple table describing the different attributes in C# and the matching data attributes. Below is table with this information.

Table

Data Validation Attributes Table

Notes

  • All data validation attributes will create a data attribute of the form data-val-<name>="<message>" and any attributes will be of the form data-val-<name>-<param>="<value>"
  • In the table the HTML Name column represents the name in the data attribute. Eg. data-val-maxlength
  • In the table the Parameter Name column represents the parameter name in the data attribute after the validation name. Eg. data-val-maxlength-max
  • The Message Index column is the index of the parameter in the ErrorMessage, index 0 is always the field name. Eg. ErrorMessage = "The field {0} cannot be longer than {1}"
  • This data is taken from the .NET Core repositories, there may be other attributes provided by other libraries

References

  • ValidationAttributeAdapterProvider
  • Data Annotation Provider Sources
like image 37
amura.cxg Avatar answered Oct 21 '22 19:10

amura.cxg