Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reinitialize jQuery unobtrusive validation?

I am loading a form with the proper attributes for jQuery via an AJAX call, the problem is because it's being loaded in after the document is ready the jQuery unobtrusive validation is not picking up the required validation. Can anyone tell me the method I would run to reinitialize it?

like image 367
Darren Avatar asked Mar 08 '12 19:03

Darren


People also ask

How does jquery unobtrusive validation work?

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. data-val-required=”This is required.”

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.


1 Answers

To parse the form's validation attributes after you return it to the page you need to run this: $.validator.unobtrusive.parse('#yourFormSelector')

In my application I put this call into the ajaxComplete handler so I don't have to worry about it anymore. I have it reparse every form on the page and it's been working fine in production for quite a while (even when there are a couple hundred forms on the page).

$(document).ajaxComplete(function() {
    $.validator.unobtrusive.parse('form');
});
like image 184
bbak Avatar answered Sep 20 '22 12:09

bbak