Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the reson behind using $("form").removeData("validator") & $("form").removeData("unobtrusiveValidation"); inside partial view

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?

like image 591
john Gu Avatar asked Jan 10 '15 02:01

john Gu


People also ask

What is jquery validate unobtrusive?

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.

How many types of validation are there in MVC?

There are two types of validations: Server side Validations. Client Side Validations.

What is validator unobtrusive parse?

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.


3 Answers

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");
}
like image 192
display name Avatar answered Oct 24 '22 02:10

display name


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.

like image 39
ramiramilu Avatar answered Oct 24 '22 02:10

ramiramilu


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 ;)

like image 24
Sophie Avatar answered Oct 24 '22 03:10

Sophie