Menu icon Foundation
Server side check on abide validators

Hey.

I've been tearing my hair for a couple of hours now verifying that I get everything right.

I two text fields and validators for them to check if the username/email is already in use in the system:

<div>
    <label>
        Username
        <input type="text" pattern="[A-Za-z0-9-_]+" data-abide-validator="userName" id="regusername" placeholder="username" required>
        <small class="error">Username might be in a invalid format (A-z 0-9 - _) or already used.</small>
    </label>
</div>
<div>
    <label>
        Email
        <input type="email" id="regemail" data-abide-validator="email" placeholder="[email protected]" required>
        <small class="error">Email either wrong format or already registered to a user.</small>
        </label>
</div>         

         

The validators looks like this:

$(document).foundation({
    abide : {
        validators: {
            userName: function(el, required, parent) {
                if ($(el).val().trim().length == 0) return false;
                $.post('/service.php?method=usernamefree', JSON.stringify({username: $(el).val().trim()}), function(e){
                    return JSON.parse(e).free;
                }).fail(function(){return false;});
            },
            email: function(el, required, parent) {
                if ($(el).val().trim().length == 0) return false;
                $.post('/service.php?method=emailfree', JSON.stringify({email: $(el).val().trim()}), function(e){
                    return JSON.parse(e).free;
                }).fail(function(){return false;});
            }
        }
    }
});

And the service returns the following data "{"free":true}" or "{"free":false}" and the call takes maximum 200ms.

Now to the problem, no matter what I return in the post it seems to be ignored. If I add a manual return true before the $.post it says valid. If I run console.log inside the post I can see the correct values, but a return true inside the $.post won't validate correct.

Is it some kind of timing issue maybe? Or is the return not getting out of the $.post?

abidevalidateserver

Hey.

I've been tearing my hair for a couple of hours now verifying that I get everything right.

I two text fields and validators for them to check if the username/email is already in use in the system:

<div>
    <label>
        Username
        <input type="text" pattern="[A-Za-z0-9-_]+" data-abide-validator="userName" id="regusername" placeholder="username" required>
        <small class="error">Username might be in a invalid format (A-z 0-9 - _) or already used.</small>
    </label>
</div>
<div>
    <label>
        Email
        <input type="email" id="regemail" data-abide-validator="email" placeholder="[email protected]" required>
        <small class="error">Email either wrong format or already registered to a user.</small>
        </label>
</div>         

         

The validators looks like this:

$(document).foundation({
    abide : {
        validators: {
            userName: function(el, required, parent) {
                if ($(el).val().trim().length == 0) return false;
                $.post('/service.php?method=usernamefree', JSON.stringify({username: $(el).val().trim()}), function(e){
                    return JSON.parse(e).free;
                }).fail(function(){return false;});
            },
            email: function(el, required, parent) {
                if ($(el).val().trim().length == 0) return false;
                $.post('/service.php?method=emailfree', JSON.stringify({email: $(el).val().trim()}), function(e){
                    return JSON.parse(e).free;
                }).fail(function(){return false;});
            }
        }
    }
});

And the service returns the following data "{"free":true}" or "{"free":false}" and the call takes maximum 200ms.

Now to the problem, no matter what I return in the post it seems to be ignored. If I add a manual return true before the $.post it says valid. If I run console.log inside the post I can see the correct values, but a return true inside the $.post won't validate correct.

Is it some kind of timing issue maybe? Or is the return not getting out of the $.post?