Small modifications to make mvc verification icing on the cake (2)

Source: Internet
Author: User

In the previous article, we demonstrated how to add some flowers to MVC verification by making minor changes to jquery. validate. unobtrusive. js.

 

The onError and onSuccess methods are modified.

These two methods are also key code used to display/hide verification information.

Based on my understanding, I added the two methods with detailed Chinese annotations.

Please comment on the following:

 

                          onError(error, inputElement) {                                   container = $().find("[data-valmsg-for='" + escapeAttributeValue(inputElement[0].name) + "']"= container.attr("data-valmsg-replace"= replaceAttrValue ? $.parseJSON(replaceAttrValue) !==  :         container.removeClass("field-validation-success field-validation-valid").addClass("field-validation-error"        error.data("unobtrusiveContainer"                        error.removeClass("input-validation-error"

 

         onSuccess(error) {                   container = error.data("unobtrusiveContainer"= container.attr("data-valmsg-replace"= replaceAttrValue ? $.parseJSON(replaceAttrValue) :             container.addClass("field-validation-success").removeClass("field-validation-error field-validation-valid"            error.removeData("unobtrusiveContainer"

 

I have not explained the above variable replace. Here I will talk about it separately:

When @ Html. ValidationMessageFor is used in the view to generate a container that displays the verification message, pay attention to the second parameter.

If the second parameter is not included

@Html.ValidationMessageFor(model => model.UserCode)

The generated html is like this, which is an empty container

 

 

If the second parameter is input

 

@ Html. ValidationMessageFor (model => model. UserCode, "Enter the user name ")

 

The generated htm is like this. It is no longer an empty container. When the verification fails, the information in the container is displayed directly, ignoring the verification information defined in the Model.

 

Enter User Name

 

It's clear now.

 

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.