1000hz / bootstrap-validator

A user-friendly HTML5 form validation jQuery plugin for Bootstrap 3
http://1000hz.github.io/bootstrap-validator
MIT License
2.38k stars 1.07k forks source link

double remote validation after form submit #486

Open cambazz opened 7 years ago

cambazz commented 7 years ago

Hello,

I had my form working nicely, but after submit tru ajax, I get the result code, make changes in my page.

But however, when I look over the network activity using developer tools, I find that after successful post, it will re-do the remote validation queries.

I tried disabling the form, even deleting it from dom tree. It will still try to remove validate.

Is there a workaround for this I wonder.

Best regards, -C

1000hz commented 7 years ago

I don't think there's a clean workaround. One potential way might be to silently remove the remove remote validators from the internal collection of validated inputs before the submit handler is fired. Something like:

$('#myForm')
  .on('submit', function (e) {
    var validator = $(this).data('bs.validator')
    validator.$inputs = validator.$inputs.not('[data-remote]') 
  })
  .validator() // initialize the validator (which sets its own submit handler)
  .on('submit', function (e) {
    $(this).validator('update') // to re-add the remote validators so future inputs get validated
  })

Not sure if this will work, but worth a shot.

delawr0190 commented 7 years ago

@cambazz Were you able to find a workaround? I tried the solution provided above from @1000hz , but it doesn't seem to solve the problem.

I'm using data-remote to check for usernames and emails that are already in use, and the second data-remote validation is causing my form to show them as "in use" briefly, before the redirect happens, because the successful POST has completed and the username and email are now registered.

delawr0190 commented 7 years ago

I'm not a Javascript expert (this may not be the ideal solution). I was able to work around my issue, though, by destroying the validator on submit if !isDefaultPrevented.

    $('#reg-form').validator().on('submit', function(e) {
        if (!e.isDefaultPrevented()) {
            // my other stuff
            $('#reg-form').validator('destroy');
        }
    });
Mucaccino commented 7 years ago

It would be great if it was resolved.