dominar icon indicating copy to clipboard operation
dominar copied to clipboard

Async rules fail and show "success" even if they get 200 OK from the api

Open vlascik opened this issue 8 years ago • 11 comments

Async rules fail and show "success" even if they get 200 OK from the api

image

        Dominar.Validator.registerAsync('username_taken', function (username, attribute, parameters, passes) {
            $.get(url, {username: username}, passes)
                .fail(function (response) {
                    passes(false, response.responseJSON.message);
                });
        });

vlascik avatar Nov 26 '15 17:11 vlascik

Dominar doesn't check what response from the server is at all, it's down to the user to call passes()

Dominar.Validator.registerAsync('username_taken', function (username, attribute, parameters, passes) {
    $.get(url, { username: username }, passes)
        .done(function() {
            passes();
        })
        .fail(function (response) {
            passes(false, response.responseJSON.message);
        });
    });
});

Also atm dominar doesn't support having a 'success message' only a failed message. I can add support for that though, would be pretty useful.

garygreen avatar Nov 26 '15 18:11 garygreen

It would be useful I guess, but at the moment I'm trying to make the validation pass (make input box green and hide the "success" message), even after I added a call to passes() in .done(), the result is the same as on picture. Edit: For some reason it seems, that the async validation rule is both in passed and failed, so AsyncResolvers.isAllResolved() returns false, as (this.passed.length + this.failed.length) is higher than this.resolversCount;

vlascik avatar Nov 26 '15 19:11 vlascik

Can you setup a fiddle or repo I can check? On 26 Nov 2015 19:41, "vlascik" [email protected] wrote:

It would be useful I guess, but at the moment I'm trying to make the validation pass (make input box green and hide the "success" message), even after I added a call to passes() in .done(), the result is the same as on picture.

— Reply to this email directly or view it on GitHub https://github.com/garygreen/dominar/issues/24#issuecomment-159984351.

garygreen avatar Nov 26 '15 20:11 garygreen

I edited the comment above with more info, did it help by any chance? But the code is the same as in your example, passes() is called. Maybe the previous failure of the same rule isn't removed?

vlascik avatar Nov 26 '15 20:11 vlascik

What does your dominar initialisation look like? On 26 Nov 2015 20:00, "Gary Hole" [email protected] wrote:

Can you setup a fiddle or repo I can check? On 26 Nov 2015 19:41, "vlascik" [email protected] wrote:

It would be useful I guess, but at the moment I'm trying to make the validation pass (make input box green and hide the "success" message), even after I added a call to passes() in .done(), the result is the same as on picture.

— Reply to this email directly or view it on GitHub https://github.com/garygreen/dominar/issues/24#issuecomment-159984351.

garygreen avatar Nov 26 '15 20:11 garygreen

var validator = new Dominar(document.getElementById('the-form'), {
    username: {
        rules: 'required|email|username_taken',
        triggers: ['keyup', 'change', 'focusout'],
        delay: 300
    }
}));

vlascik avatar Nov 26 '15 20:11 vlascik

I can't seem to replicate the issue, seems to work fine for me. http://jsfiddle.net/18yb3aja/

garygreen avatar Nov 26 '15 20:11 garygreen

I've setup a fiddle which shows the problem here: http://jsfiddle.net/dnran7da/4/ I had to upload own dominar-standalone.js that i had to manually npm build from yesterday's master (before your latest commits), as the one in dist/ is not updated, maybe that could be the issue?

vlascik avatar Nov 26 '15 21:11 vlascik

If you put passes as the third parameter to .get you need to make sure you don't return 200 for the .fail to be called when validation should fail.

garygreen avatar Nov 26 '15 21:11 garygreen

Yep, that was it, it works now, also a call to .done(function() { passes() is not mentioned there. Thanks.

vlascik avatar Nov 26 '15 21:11 vlascik

Also, it should say:

              .done(function() {
                    passes();
              }).
                .fail(function (response) {
                    passes(false, response.responseJSON.message);
                });

to show response message, response.message will be undefined

vlascik avatar Nov 26 '15 21:11 vlascik