jbojcic
jbojcic

Reputation: 973

Parsley 2.0 remote not triggered on submit

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(...);
        }
}

Upvotes: 2

Views: 1337

Answers (1)

guillaumepotier
guillaumepotier

Reputation: 7448

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'); });
}

Upvotes: 2

Related Questions