Jquery validation: run remote confirmation again without changing the value of the corresponding field

I have a remote verification setting

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 the bypass is yes, ajax always returns true.

It works when blurring and when sending, but if I reset the value of the radio button, then send it, it will not revalidate.

For instance,

  • Set toggleBypass to "yes"
  • Specify a field for an invalid value
  • Run $ ("form"). validate (). element ("# field"). Since the bypass is yes, it passes.
  • Set toggleBypass to "no"
  • Run $ ("form"). validate (). element ("# field"). It passes because the remote has not resent.

Any ideas how I can make it work correctly?

+4
source share
1 answer

If you look at line 897 in jquery.validate.js, you will see the optimization:

if ( previous.old !== value ) {

This line prevents a new check if the field value has not changed. And since you use validate().element() , it validates this field, not the entire form, so it does not detect changes in toggleBypass.

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

+2
source

Source: https://habr.com/ru/post/1347209/


All Articles