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.