Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery validation: get remote validation running again without changing the value of concerned field

I hava a remote validator setup like so

remote: {
  type: "POST",
  url: "/some/url",
  data: {
    value: function() {
      return $("#field").val();
    },
    bypass: function() {
      if ($("input:radio[name=toggleBypass]:checked").val() == "yes"){
        return "yes";
      }
      return "no";
    }
  }
}

If bypass is "yes", the ajax always returns true.

It works on blur and on submit, but if I reset the radio button values then submit, it doesn't revalidate.

For example,

  1. Set toggleBypass to "yes"
  2. Set field to an invalid value
  3. Run $("form").validate().element("#field"). Since bypass is "yes", it passes.
  4. Set toggleBypass to "no"
  5. Run $("form").validate().element("#field"). It passes, because the remote didnt resubmit.

Any ideas how I can get it to work properly?

like image 230
Altealice Avatar asked Nov 05 '22 00:11

Altealice


1 Answers

If you look at line 897 in jquery.validate.js, you'll see an optimization:

if ( previous.old !== value ) {

This line prevents a new validation if the field's value hasn't changed. And since you're using validate().element(), it only checks that field, not the whole form, so it doesn't detect the change in toggleBypass.

Either remove this check from the jquery.validate source, or check the whole form, not just that field.

like image 156
Nikolay Yordanov Avatar answered Nov 15 '22 00:11

Nikolay Yordanov