jQuery 验证器远程方法

jQuery validator remote method

我正在使用远程验证方法,对 http://jqueryvalidation.org/remote-method/ 的以下部分有疑问。

响应被评估为JSON,对于有效元素必须为真,对于无效元素可以是任何假,未定义或空,使用默认消息;或字符串,例如。 "That name is already taken, try peter123 instead" 显示为错误消息。

如果服务器echo('custom error');没有通过验证,但不会显示错误。为什么不呢?

如果服务器执行echo(null);echo(false);,或者根本没有回显,客户端似乎没有得到响应,没有通过验证,并且不显示默认消息。它不应该显示默认消息吗?同样,如果服务器执行 echo('undefined');,客户端会收到 'undefined',但不会显示默认消息。

服务器脚本

<?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');
?>

客户端脚本

<!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>

"If the server echo('custom error');, it does not pass validation, but the error is not displayed. Why not?"

因为您需要将服务器响应作为 JSON 字符串发回;不是常规字符串,也不是布尔值。

echo json_encode('this is going to be the error message');

这将通过验证...

echo json_encode('true');

这将导致验证失败并使用默认消息...

echo json_encode('false');

这些也可以像上面那样工作...

echo 'true'  // pass
echo 'false' // fail using default message

这行不通...

echo 'custom message';

//The following results in the client receiving 1, and "1" is displayed as error [snip] ....

文档:

"The response is evaluated as JSON and must be... any false, undefined or null for invalid elements"

任何不被解释为包含 "true" 的字符串都将无法通过验证。 由于您没有将这些响应回显为 JSON,事实证明您的各种错误消息非常难以预测。

是的,我同意文档对 JSON 的措辞有点含糊。 "evaluated as JSON" 似乎暗示插件正在进行转换。不是这种情况。服务器响应必须在评估时已经JSON。


type:'get'

GET 已经是 remote 方法的默认值,不需要指定。


我创建了一个 pull request 来将文档更改为:

The serverside response must be a JSON string that must be "true" for valid elements, and can be "false", undefined, or null 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.