I am reading the following example link about displaying a partial view inside a popup menu. but i have noticed that inside the partial view the author uses the following code at the end of the view:-
$("form").removeData("validator");
$("form").removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse("form");
so can anyone advice what is the purpose behind adding this code?
Unobtrusive Validation means without writing a lot of validation code, you can perform simple client-side validation by adding the suitable attributes and including the suitable script files. These unobtrusive validation libraries need to be added: jquery.validate.min.js.
There are two types of validations: Server side Validations. Client Side Validations.
validator. unobtrusive. parse(selector) method to force parsing. This method parses all the HTML elements in the specified selector and looks for input elements decorated with the [data-val=true] attribute value and enables validation according to the data-val-* attribute values.
It removes the jQuery validation from the form. Here is a reference to the validation data.
var form = $(formSelector)
.removeData("validator") /* added by the raw jquery.validate plugin */
.removeData("unobtrusiveValidation");
/* added by the jquery unobtrusive plugin */
To be specific to the implementation in partial view, you can implement the validation with a method like this
function ApplyValidation() {
$("form").removeData("validator");
$("form").removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse("form");
}
In unobtrusive validation, once the validators have been applied for a document, any other validators of dynamic content(partial views or jquery induced html controls) will not be applied. Once we reload the validators, it will bind rules defined inside the model with rules implementation provided by JQuery library, so validation will be performed seamlessly.
The other option (instead of reloading validators) you have is to inject the new rules as shown here.
Access the form's unobtrusiveValidation
data using the jquery data method ($(form).data('unobtrusiveValidation'))
and access the rules collection and add the new elements attributes.
In my case I use $("form").removeData("validator") because I have 2 differents validations for the same form.. calling different validation according the button has pressed ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With