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>