php - jQuery 验证器远程方法

标签 php jquery jquery-validate

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

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

如果服务器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 字符串发送回来;不是常规字符串,也不是 bool 值。

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 措辞有点含糊。 “评估为 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.

关于php - jQuery 验证器远程方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29775304/

相关文章:

javascript - 正则表达式 - 查找字符串中的所有匹配项

Bootstrap Modal 中的 jQuery 验证不起作用

php - HTMLPurifier 删除目标 ="_blank"

php - doctrine2 原生查询更新语句

php - JavaScript 不工作 - apache2 + Ubuntu

jquery - 如何对子域进行 jquery ajax 调用?

javascript - 在第一次加载时,用户会在动画开始之前看到动画结束的一瞥

javascript - jQuery 验证规则不适用于嵌套 html 标签

jQuery 验证插件条件必填字段

php - 如何在自动建议中避免与 MySQl 重复