asp.net MVC model verification based on callout feature: Dataannotationsmodelvalidator

Source: Internet
Author: User
Tags definition

For asp.net MVC's model validation based on annotation features, many people only know the validationattribute that is used to define validation rules and error messages on data types and their properties. Through the "ASP.net mvc modelvalidator as the core model verification system: Modelvalidator" Introduction, we know that the final model for the validation of a component called Modelvalidator. Validationattribute corresponds to the modelvalidator for Dataannotationsmodelvalidator, this brief article introduces you to ASP.net How MVC creates Dataannotationsmodelvalidator for validation, and how the latter uses the former to implement model validation.

First, Dataannotationsmodelvalidator

Modelvalidator is the component that is really used for model validation, and the validation features described above are eventually encapsulated into Dataannotationsmodelvalidator objects and then applied to the model validation system. As shown in the following code fragment, the encapsulated Validationattribute is represented by a read-only property attribute, which is initialized in the constructor.

1:public class Dataannotationsmodelvalidator:modelvalidator
2: {
3:public dataannotationsmodelvalidator (Modelmetadata metadata, ControllerContext context, Validationattribute attrib UTE);
4:public override ienumerable<modelclientvalidationrule> Getclientvalidationrules ();
5:
6:public override ienumerable<modelvalidationresult> Validate (Object container)
7: {
8:validationcontext validationcontext = new Validationcontext (container?? this. Metadata.model, NULL, NULL)
9: {
10:displayname = this. Metadata.getdisplayname ()
11:};
12:validationresult Validationresult = this. Attribute.getvalidationresult (this. Metadata.model, Validationcontext);
13:if (Validationresult!= validationresult.success)
14: {
15:modelvalidationresult iteratorVariable2 = new Modelvalidationresult
16: {
17:message = Validationresult.errormessage
18:};
19:yield return iteratorVariable2;
20:}
21:else
22: {
23:yield break;
24:}
25:}
26:protected Validationattribute Attribute {get;}
27:protected string errormessage {get;}
28:public override bool IsRequired {get;}
29:}

We give a complete definition of the core method validate for implementing validation. In this method, a Validationcontext object representing the current validation context is created based on the validated object (if NULL is the model attribute of the model metadata), and the DisplayName property of the model metadata is used as the display name for the context. Finally, the Getvalidationresult method of the encapsulated Validationattribute is directly invoked to validate the specified object, if the returned Validationresult object is not empty, The Modelvalidationresult object is created and returned.

By the way, the logic of the two other protected read-only properties defined in Dataannotationsmodelvalidator. The ErrorMessage property source that returns the error message calls to the Formaterrormessage method of the Validationattribute, and the specified parameter is the DisplayName property of the current model metadata. Because only RequiredAttribute is used for validation of required fields, all isrequired properties return true only if they are encapsulated Validationattribute as RequiredAttribute.

Second, dataannotationsmodelvalidator<tattribute>

Dataannotationsmodelvalidator<tattribute> is a subclass of Dataannotationsmodelvalidator, Its generic parameter is the corresponding Validationattribute type, and the following code fragment reflects its definition:

   1:public class dataannotationsmodelvalidator<tattribute>: Dataannotationsmodelvalidator where TAttribute: Validationattribute
2: {
3: Public dataannotationsmodelvalidator (modelmetadata metadata, Modelbindingexecutioncontext context, Tattribute attribute);
4: protected tattribute Attribute {get;}
5:}

As an adaptation between Dataannotationsmodelvalidator and corresponding Validationattribute, asp.net mvc is a commonly used Validationattribute (RequiredAttribute , Rangeattribute, Regularexpressionattribute, and Stringlengthattribute) define the appropriate fit type. As shown in the following code snippet, they are all dataannotationsmodelvalidator<tattribute> subclasses of the generic type. When we apply these validationattribute to model types, the actual model validation is actually the modelvalidator of these adaptations.

1:public class Requiredattributeadapter:dataannotationsmodelvalidator<requiredattribute>
2: {
3:public requiredattributeadapter (Modelmetadata metadata, ControllerContext context, RequiredAttribute attribute);
4:public override ienumerable<modelclientvalidationrule> Getclientvalidationrules ();
5:}
6:
7:public class Rangeattributeadapter:dataannotationsmodelvalidator<rangeattribute>
8: {
9:public rangeattributeadapter (Modelmetadata metadata, ControllerContext context, rangeattribute attribute);
10:public override ienumerable<modelclientvalidationrule> Getclientvalidationrules ();
11:}
12:
13:public class Regularexpressionattributeadapter:dataannotationsmodelvalidator<regularexpressionattribute >
14: {
15:public regularexpressionattributeadapter (Modelmetadata metadata, ControllerContext context, Regularexpressionattribute attribute);
16:public override ienumerable<modelclientvalidationrule> Getclientvalidationrules ();
17:}
18:
19:public class Stringlengthattributeadapter:dataannotationsmodelvalidator<stringlengthattribute>
20: {
21:public stringlengthattributeadapter (Modelmetadata metadata, ControllerContext context, Stringlengthattribute attr Ibute);
22:public override Ienumerable<modelclientvalidationrule>getclientvalidationrules ();
23:}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.