Reputation: 26281
I am using the remote validation method and have questions about the following section of http://jqueryvalidation.org/remote-method/.
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; or a string, eg. "That name is already taken, try peter123 instead" to display as the error message.
If the server echo('custom error');
, it does not pass validation, but the error is not displayed. Why not?
If the server executes echo(null);
or echo(false);
, or doesn't have an echo at all, the client doesn't appear to get a response, it doesn't pass validation, and the default message isn't displayed. Shouldn't it display the default message? Similarly, if the server executes echo('undefined');
, the client receives 'undefined' but the default message isn't displayed.
Server Script
<?php
header('Content-Type: text/plain;');
//The following passes validation
//echo('true');
//The following results in the client receiving 1, and "1" is displayed as error
//echo(true);
//echo(1);
//echo('1');
//The following will trigger the default message
//echo(0);
//echo('null');
//echo('false');
//echo('0');
//The following results in no ajax response to client, and no message is displayed.
//Doesn't this result in client getting undefined which should display the default message
//echo(null);
//echo(false);
//no echo at all
//Client receives "undefined", but it doesn't display the default message. Shouldn't it?
//echo('undefined');
//Client receives "custom error", but it doesn't display this text. Shouldn't it?
echo('custom error');
?>
Client Script
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Testing</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.13.1/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
var validator=$("#myForm").validate({
rules: {
bla: {
minlength:2,
maxlength:4,
required:true,
remote: {url:"validate.php",type:'get',data:{a:1,b:2,c:3}}
//remote: "validate.php"
}
},
messages: {
bla: {
remote:"default message"
}
}
});
});
</script>
</head>
<body>
<form id="myForm" method="post">
<input name="bla" id="bla" value="">
<input type="submit" value="submit">
</form>
</body>
</html>
Upvotes: 3
Views: 3811
Reputation: 98728
"If the server echo('custom error');, it does not pass validation, but the error is not displayed. Why not?"
Because you need to send the server response back as a JSON string; not a regular string and not a boolean.
echo json_encode('this is going to be the error message');
This is going to pass validation...
echo json_encode('true');
This is going to fail validation and use the default message...
echo json_encode('false');
These will also work as above...
echo 'true' // pass
echo 'false' // fail using default message
This will NOT work...
echo 'custom message';
//The following results in the client receiving 1, and "1" is displayed as error [snip] ....
Docs:
"The response is evaluated as JSON and must be... any false, undefined or null for invalid elements"
Anything not being interpreted as a string containing "true"
, will FAIL validation. Since you're not echoing these responses as JSON, your various error messages are proven to be very unpredictable.
Yes, I agree the documentation is a bit ambiguous about the JSON wording. "evaluated as JSON" seems to imply that the plugin is doing the conversion. This is not the case. The server response must already be JSON when it's evaluated.
type:'get'
GET
is already the default for the remote
method and does not need to be specified.
I created a pull request to get the documentation changed into this:
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.
Upvotes: 5