Search code examples
jqueryvalidationparsley.jsremote-validationparsley

Parsley Validation not waiting for Remote Validation AsyncValidate to finish


I have a form and I'm trying to do parsley remote validation on one of the fields, however, on form validation, the form submits before it waits for a response from the remote function (via asyncValidate()). Thus, even when the field is invalid, it'll show the field error css but the form still submits.

I tried using a $.Deferrede object but this isn't working.

function add_remote_validation() {

    var form = card.el.find('form');

    $(form).parsley().addAsyncValidator('validate_function', function (xhr) {

        return parseInt(xhr.responseText);
    });
}

add_remote_validation();

card.cards['my_card1'].on('validate', function(card){

    add_remote_validation();

    var result = $.Deferred();

    // test any asyncronous validation before doing regular validation
    $(form).parsley().asyncValidate()
        .done(function(){

            var valid = $(form).parsley().validate();

            if(valid) {

                result.resolveWith(valid);
            } else {

                result.rejectWith(valid);
            }
        })
        .fail(function(){

            result.rejectWith(false);
        });
});

Solution

  • It's hard to tell if you need the addAsynchValidator at all (your code returns an integer, it should return true or false). If you do, you should call window.Parsley.addAsyncValidator (as you are told in a warning). The rest of the code should not be needed.

    I'm assuming you are using the latest version.