【问题标题】:jQuery validator remote methodjQuery 验证器远程方法
【发布时间】:2015-04-21 14:35:17
【问题描述】:

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

响应被评估为 JSON 并且对于有效元素必须为 true,对于无效元素可以是任何 false、undefined 或 null,使用默认消息;或字符串,例如。 “该名称已被占用,请尝试 peter123”以显示为错误消息。

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

如果服务器执行echo(null);echo(false);,或者根本没有回显,则客户端似乎没有得到响应,它没有通过验证,并且默认消息不是显示。它不应该显示默认消息吗?同样,如果服务器执行echo('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>

【问题讨论】:

  • 由于已经有很多关于使用 remote 方法混淆的问题,我几乎将其作为 this 的副本关闭。但是,您的更具体一点,也许将来可以用来关闭其他人。

标签: php jquery jquery-validate


【解决方案1】:

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

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

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

这将通过验证...

echo json_encode('true');

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

echo json_encode('false');

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

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

这行不通...

echo 'custom message';

//下面的结果是客户端收到1,“1”显示为error [snip] ....

文档:

“响应被评估为 JSON 并且必须是...任何 false、未定义或无效元素的 null”

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

是的,我同意文档对 JSON 的措辞有点模棱两可。 “评估为 JSON” 似乎暗示插件正在进行转换。不是这种情况。服务器响应在评估时必须已经是 JSON。


type:'get'

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


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

服务器端响应必须是 JSON 字符串,对于有效元素必须是 "true",对于无效元素可以是 "false"undefinednull,使用默认错误消息。如果服务器端响应是一个字符串,例如。 "That name is already taken, try peter123 instead",此字符串将显示为自定义错误消息代替默认值。

【讨论】:

  • 这让我发疯了!谢谢
  • @user1032531,我认为很多人只是因为文档中的措辞而遇到问题。我已提议对文档进行更改,如我上面的编辑中所述。请随时在the GitHub thread 上对此发表评论。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多