Question

I use remote validation for user registration to check is email already used and it works ok on change but validation is not triggered on submit. So if users enters email that is already used he will get error message but when he submits request that error is ignored and ajax call will is executed.

This is my email input:

<div id="userPanel">
     ...
     <input id="emailInput" class="form-control" type="text" name="email" data-parsley-type="email" data-parsley-required="true" data-parsley-trigger="change" data-parsley-remote="@Url.Action("CheckIsEmailUnique", "Onboarding")" data-parsley-remote-message="Email must be unique."/>
     ...
</div>

Javascript:

var parsleyForm = $('#userPanel').parsley();
$("#saveNewUser").click(function () {
        var isValid = parsleyForm.validate();
        if (isValid) {
               ...
               ajaxCall(...);
        }
}
Était-ce utile?

La solution

When you use Parsley.remote.js, two new methods are available: asyncValidate() and asyncIsValid() that returns promises.

Parsley.remote.js auto-bind asyncValidate() on data-parsley-trigger events and on form submit.

If you want to manually control parsley on form submit via javascript, please use this:

var parsleyForm = $('#userPanel').parsley();
$("#saveNewUser").click(function () {
    parsleyForm.asyncValidate()
      .done(function () { console.log('success'); })
      .fail(function () { console.log('there is an error'); })
      .always(function () { console.log('done everytime whatever happens'); });
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top