views:

69

answers:

1

Hi All

I'm using jQuery Validation plugin to validate a form. Do you know how to force revalidation on previously successful fields?

I have tried the .form function using the following check (this is performed after the user clicks 'submit'):

if ($('#form1').validate().form()==false)
{
    formValid = false;
}

However, it appears that the code above does not retry validation so fields that are already successfully validated (ie have tick next to them) are not checked again.

The reason for wanting to retry revalidation on previously successful fields is that they rely on remote validation, and the outcome (success or failure) can change between the user leaving the field, and clicking submit. (This applies to a 'username' field).

In case it affects the answer I have multiple forms to validate (for simplicity, in the code snippet above I refer to '#form1' only).

Thanks in advance for any advice,

Rob

A: 

The state of validation for remote fields is stored via $.data() with the element you want to validate, so you could use .removeData() to clear that out...so it's forced to revalidate:

$("#form1 :input").removeData("previousValue");
//now call .valid()

This forces the check if the value has changed (we need to re-validate) to be true:

//This code is in the validation plugin for remote:
var previous = this.previousValue(element);
if (previous.old !== value) { //this is normally false, since it hasn't changed

If there are only specific fields that need re-validating, like you said username, you may want to narrow the $("#form1 :input") selector to only the fields you want, to make it a bit more efficient.

Nick Craver
Excellent - this clears the validation perfectly, thanks Nick... although another problem has cropped up! Because it is now revalidating all the forms I get an error in javascript: "Failed to load resource: cancelled". The problem only occurs when I have multiple forms on the page (which is a user-driven action, as the forms are created dynamically). Any suggestions?
Rob
@Rob - I would clear only the remote options with this, it'll save the most headaches, can you do that with a selector more specific than I have above? Also I'd only re-validate the one you're submitting.
Nick Craver
@Nick - from some debugging it appears that the "Failed to load resource" error happens when .validate().form() is called simultaneously for two form inputs with the same name. I need to check all inputs on submit so even having the most specific selector still produces the issue. The error does not occur if I call .validate().form() manually on each form sequentially with a user-driven action (eg clicking a 'validate' button)... so it seems I need to validate the form in sequence (i.e. only call the validation of the next form upon successful validation of the first)... any suggestions?
Rob
@Rob - I'm not sure exactly, you're describing a very strange problem...do you have a page I can take a look at to see what's going on?
Nick Craver