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).
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.
The documentation for the remote
method states:
The serverside response must be a JSON string that must be
"true"
for valid elements, and can be"false"
,undefined
, ornull
for invalid elements, using the default error message. If the serverside response is a string, eg."That name is already taken, try peter123 instead"
, this string will be displayed as a custom error message in place of the default.
Quote OP:
"The script
does_user_exist.php
returns1
if the user exists."
This might be the opposite of what you want. Since 1
may be getting interpreted as true
, which tells the plugin it "passed" validation. As per docs, if the user exists and you want to trigger an error message, the response must be false
, undefined
, null
, or a string (the message).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With