Search code examples
jqueryjquery-validate

Error response from jQuery Validate remote isn't working


I am using the remote method of jQuery Validation to check if a username already exists in the system. The script does_user_exist.php returns 1 if the user exists.

$registration.find(".username").each(function() {
    var $this = $(this);
    $this.rules('add', {
        remote: {
            url: "does_user_exist.php",
            type: "post",
            dataType: "json",
            data: {uname: function(){ return $this.val(); } },
            async: false,
            success: function (data) { //returns 1 if user exists
                if (data) {
                   console.log("false!");
                   return "Sorry, that user already exists." //doesn't work
                   //return false;        //doesn't work
                   //return data;         //doesn't work
                   //return {"string"};   //doesn't work
                   //return {0: "string"};//doesn't work
                } else {
                   console.log("true!");
                   return true;
                }
            }
        },
        messages: {
            remote: jQuery.format("{0}") //doesn't work
            // remote: "Simple string doesn't work here either"
        }
    });
});

My problem is that the error message is never displayed. I can see from my console.log output that it correctly logs false! when the user exists, but the error message never shows up.

As you can see from the commented out lines, I've been trying to figure out what exactly I'm doing wrong via guess-and-check, on the assumption that I'm somehow returning the error string in the wrong format, but I'm not having success.

Edit3: I was able to get the backend response to be changed. The PHP is now sending JSON encoded true/false, to no avail. We have tried sending the backend response in the following ways, but the plugin doesn't seem to be catching it no matter what we do:

json_encode( false )
json_encode( '["false"]' )
json_encode( "false" )
json_encode( ["false"] )
"false"
false
NULL
""
0

As far as I can tell based on the documentation, one of these should have worked:

The response is evaluated as JSON and must be true for valid elements, and can be any false, undefined or null for invalid elements, using the default message;

Note, the answer to this question may be related to this issue.

Here is the revised code:

$registration.find(".username").each(function() {
    var $this = $(this);
    remote: {
        url:  "does_user_exist.php",
        type: "post",
        data: {entityExistence: function(){return $this.val();} },
        success: function(data){ console.log(data); }
    },
    messages: {
        remote: "Sorry, that username is not available."
   }
});

The PHP script is always returning true or false correctly. According to the documentation, anything except for true should trigger the error message. But the error message doesn't come up. I know error messaging is working because there are other checks are working fine (e.g. rangelength, removed from pasted-in code here for clarity).


Solution

  • Ok, finally figured it out, randomly. The success callback was apparently ruining everything. All I had to do was remove this:

    success: function(data){
        console.log(data);
    }
    

    As soon as I did that, everything worked perfectly as expected using simple string responses:

    "true"
    

    and

    "false"
    

    I can not explain this, but I hope that someone updates the documentation! I noticed that using complete does not break it the way that success does.