jQuery验证器远程方法

时间:2015-04-21 14:35:17

标签: php jquery jquery-validate

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

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

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

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

服务器脚本

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

1 个答案:

答案 0 :(得分:5)

  

“如果服务器回显('自定义错误');,它没有通过验证,但是没有显示错误。为什么不呢?”

因为您需要将服务器响应作为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';

  

//以下结果是客户端收到1,“1”显示为错误[snip] ....

文档:

  

“响应被评估为JSON,并且必须是......对于无效元素,任何false,undefined或null”

任何未被解释为包含"true"的字符串的内容都将进行失败验证。 由于您没有将这些响应作为JSON回显,因此您的各种错误消息被证明是非常不可预测的。

是的,我同意文档对JSON措辞有点含糊不清。 “评估为JSON”似乎意味着该插件正在进行转换。不是这种情况。服务器响应必须在评估时已经是JSON。


type:'get'

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


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

  

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