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?
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.”
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.
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');
});
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