How To : Reset the Validation Rules on a Dynamic Form (JQuery + Validation Plugin)

September 24, 2009 by Reboot · 1 Comment 

I created an application which handles requests, after choosing a request type the form fields are fetched and a form is generated. It’s completely AJAX based, thus the page is never refreshed. I ran into a problem though, rules from previous form generations persisted between request changes. The answer is simple, add an empty “rules” property to the validate() call. Note that internally the Validate plugin stores the form validation information in the JQuery data space so my first shot was to erase this data, which didn’t work.

—————- code snippet from jquery.validate.js —————-

line 35 $.data(this[0], ‘validator’, validator);

—————- code snippet —————-

myform.validate({
rules: {}, // REQUIRED TO RESET THE RULES LIST!!
onkeyup: false,
onfocusout: false,
onclick: false,

submitHandler: function(form) {

—————- ajax-based validation method example —————-

jQuery.validator.addMethod(“validatepair”, function(value, element) {

var checkresult = false;
$.ajax({

url: “validation_url”,
async: false, // important!
data: {
“value1″:$(‘#value1id’).val(),
“value2″:$(‘#value2id option:selected’).val()

},
dataType: “jsonp”,
success: function(data){
checkresult = data.valid;
}

});

return checkresult;
}, “Value1 does not match with the choosen value2 option.”);

About Reboot
Software Engineer - http://www.linkedin.com/in/orlissenberg

Comments

One Response to “How To : Reset the Validation Rules on a Dynamic Form (JQuery + Validation Plugin)”
  1. Uday says:

    I too have a similar problem .. MY form is also ajax based.
    There is a resetForm() method (line 374) in the jquery.validate.js file but that also doesnt work. I got a cancel button in my form, which calls this method. But when i load the form again, the validation doesnt work.
    I had to remove the error labels, CSS style manually.. which got it working but I dont know if that solution is reliable.

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!