jQuery远程验证器服务器生成的错误消息未显示

时间:2016-01-27 13:55:33

标签: jquery jquery-validate

我正在使用jQuery验证器来验证表单。如果没有使用电子邮件,我的服务器将返回true,如果正在使用,则返回this email is already being used之类的内容。使用Firebug,我看到对jQuery验证器的GET请求的响应为truethis email is already being used,我相信服务器工作正常。

但是,当服务器返回这些错误时,它们不会显示为错误。但是,正在显示其他非远程错误,例如无效的电子邮件。虽然没有显示远程错误,但它们实际上阻止了表单的提交。

如下图所示,我也使用自定义客户端消息,但我已经删除了他们认为他们可能负责,但得到相同的结果。

EDIT。我的服务器返回'true'表示没有错误,'some error message'表示错误。但是,如果我的服务器返回'false',则会显示客户端消息。

如何让jQuery验证程序显示服务器生成错误消息?

{
    "ignore": [],
    "rules": {
        "email": {
            "email": true,
            "maxlength": 45,
            "invite":true,
            "remote": "openContactEmail"
        }
    },
    "messages": {
        "email": {
            "email":"Please enter a valid email address.",
            "remote":"Another contact is using this email for this account."
        }
    },
    "data": {
        "email": {
            "id": 123
        }
    },
    "remote": "path_to_validation_file.php"
}

2 个答案:

答案 0 :(得分:0)

您可以像这样配置远程返回带有isError和消息的json。

然后配置dataFilter函数以返回消息。

 remote: {
        url: "path_to_validation_file.php"
},
        dataFilter: function(data) {
            var json = JSON.parse(data);
            if(json.isError == "true") {
                return "\"" + json.errorMessage + "\"";
            } else {
                return '';
            }

        }
    }

他们也在推荐this

答案 1 :(得分:0)

我的服务器一直在返回:

echo($error?'true':$message);

我将其更改为以下内容,现在正在显示服务器生成的消息。

echo($error?json_encode(true):json_encode($message));

http://jqueryvalidation.org/remote-method/

  

服务器端响应必须是必须为“true”的JSON字符串   有效元素,可以是“false”,undefined或null,表示无效   元素,使用默认错误消息。我

相关问题