简体   繁体   中英

Custom Parsley.js asynchronous validator loops twice

I have been figuring this out for the whole afternoon and I can't seem to be able to debug this. The problem is that when I do a hard refresh of the page (document ready fires) my custom async validator runs twice but only posts one time to the server.

window.Parsley.addAsyncValidator('emailvalidation', function (data) {
  console.log("Runs");

  var myResponseText = data.responseText;
  var obj = jQuery.parseJSON(myResponseText);

  valid = (obj.result == 1);
  return valid;
}, '/check.do?action=userEmailAvailable&ajax=1');

Basically it's gonna print Runs twice in the console, but it's not gonna repeat the server post. For each time it loops it also prints one more error message (duplicate). This does not happen if I come to the page from a link (using smoothState and ajax dom insertion so document ready doesn't fire). The submit form is at the bottom of the page and is not wrapped into document ready and neither is the validator which is in a separate .js file (also not wrapped in document ready). This problem only occurs with this custom validator (which is also the only one that is an ajax one).

Best regards

No time to check the source right now, but it's entirely possible the validator will be run more than once per request. In particular, ajax requests are be cached....

This should not be an issue though, your code is simply a translation from the ajax response to the end result (is it valid), running it multiple times should not have any impact.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM