1

My question is more process based than code based, but to share (and maybe stumble upon some inconsistencies) I am using:

  • jQuery 1.6.2
  • jQuery Validations 1.8.1

So here goes...

When adding in a custom validation (using addMethod) that requires a call to the server (instead of the remote call built in), you must set to response to happen synchronously because the method will finish before the result returns from the server. Is this correct?

I'm also assuming the 2 main ways to do this are via calling an $.ajax request with async false, or deferred.promise(). Is this correct?

I've been looking thru many posts that ask questions related to custom validation using getJSON and the responses are usually "you're not returning anything back" but doesn't this miss the point - isn't the response too late as well?

Thanks for any responses!

1 Answer 1

2

Your assumptions are correct. You cannot use asynchronous requests with custom methods because the jQuery validate needs to know that this is an async request. That's why you should use the remote rule in this situation and not custom methods:

$("#myform").validate({
    rules: {
        email: {
            required: true,
            email: true,
            remote: {
                url: "check-email.php",
                type: "post",
                data: {
                    username: function() {
                        return $("#username").val();
                    }
                }
            }
        }
    }
});

Synchronous AJAX requests (or SJAX) are not something that I would recommend you doing.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Darin - I'm on Rails and trying to keep my routes and controllers as DRY as possible, so I do not want to create new actions to deal with a simple validation check. I'm using a GET and passing some params that get parsed by my search method to return an array and just check if the response is empty or not. I guess with this type of complexity I HAVE to use a custom method instead of the remote, correct?
@sshefer, I don't know Rails but why would you need to create different actions to perform validation checks? Can't you reuse the same action? Your action should return true or false depending on whether the validation passed or failed. So define an action which takes as input all the parameters it requires and have it return a boolean value.
thats a great idea, my only issue/problem is can I override the remote method so that I can explicitly tell it to pass? I know by default it includes the field name and value in the call PLUS the data you define. It would be ideal if I could set the params myself.
After a solid 53 min of contemplation you've won me over. RESTful routes and minimal actions sometimes have to take a back seat to practically and ease of use (read: why the hell am I wasting time thinking about this when I could be getting things done). Thanks! Note - if you know of a way to bypass the default params I would really appreciate that as well :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.