garygreen / dominar

Lightweight bootstrap validator inspired by Laravel
http://garygreen.github.io/dominar/
22 stars 4 forks source link

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

Open vlascik opened 8 years ago

vlascik commented 8 years ago

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);
                });
        });
garygreen commented 8 years ago

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.

vlascik commented 8 years ago

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;

garygreen commented 8 years ago

Can you setup a fiddle or repo I can check? On 26 Nov 2015 19:41, "vlascik" notifications@github.com 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.

vlascik commented 8 years ago

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?

garygreen commented 8 years ago

What does your dominar initialisation look like? On 26 Nov 2015 20:00, "Gary Hole" holegary@gmail.com wrote:

Can you setup a fiddle or repo I can check? On 26 Nov 2015 19:41, "vlascik" notifications@github.com 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.

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

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

vlascik commented 8 years ago

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?

garygreen commented 8 years ago

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.

vlascik commented 8 years ago

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

vlascik commented 8 years ago

Also, it should say:

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

to show response message, response.message will be undefined