我正在使用
jQuery Validation的
remote
方法来检查系统中是否已存在用户名。如果用户存在,脚本 does_user_exist.php
返回 1
。
$registration.find(".username").each(function() {
var $this = $(this);
$this.rules('add', {
remote: {
url: "does_user_exist.php",
type: "post",
dataType: "json",
data: {uname: function(){ return $this.val(); } },
async: false,
success: function (data) { //returns 1 if user exists
if (data) {
console.log("false!");
return "Sorry, that user already exists." //doesn't work
//return false; //doesn't work
//return data; //doesn't work
//return {"string"}; //doesn't work
//return {0: "string"};//doesn't work
} else {
console.log("true!");
return true;
}
}
},
messages: {
remote: jQuery.format("{0}") //doesn't work
// remote: "Simple string doesn't work here either"
}
});
});
我的问题是错误消息永远不会显示。我可以从我的
console.log
输出中看到,当用户存在时,它会正确记录 false!
,但错误消息永远不会显示。
正如您从注释掉的行中看到的那样,我一直在尝试通过猜测和检查找出我到底做错了什么,假设我以某种方式以错误的格式返回错误字符串,但我没有成功。
编辑3: 我能够改变后端响应。 PHP 现在发送 JSON 编码的
true
/false
,但无济于事。我们尝试通过以下方式发送后端响应,但无论我们做什么,插件似乎都无法捕获它:
json_encode( false )
json_encode( '["false"]' )
json_encode( "false" )
json_encode( ["false"] )
"false"
false
NULL
""
0
据我所知,根据文档,其中之一应该有效:
响应被评估为 JSON,并且有效元素必须为 true, 对于无效元素,可以是任何 false、未定义或 null,使用 默认消息;
注意,这个问题的答案可能与这个问题有关。
这是修改后的代码:
$registration.find(".username").each(function() {
var $this = $(this);
remote: {
url: "does_user_exist.php",
type: "post",
data: {entityExistence: function(){return $this.val();} },
success: function(data){ console.log(data); }
},
messages: {
remote: "Sorry, that username is not available."
}
});
PHP 脚本始终正确返回
true
或 false
。根据文档,除了 true
之外的任何内容都应该触发错误消息。但错误消息没有出现。我知道错误消息传递正在起作用,因为还有其他检查工作正常(例如 rangelength
,为了清楚起见,从此处粘贴的代码中删除了)。
好吧,终于想通了,随机的。
success
回调显然毁了一切。我所要做的就是删除这个:
success: function(data){
console.log(data);
}
一旦我这样做了,使用简单的字符串响应一切都按预期完美运行:
"true"
和
"false"
我无法解释这一点,但我希望有人更新文档!我注意到使用
complete
不会像 success
那样破坏它。
方法的文档指出:
服务器端响应必须是 JSON 字符串,对于有效元素必须为
,对于无效元素可以为"true"
、"false"
或undefined
,使用默认错误消息。如果服务器端响应是一个字符串,例如。null
,此字符串将显示为自定义错误消息,而不是默认值。"That name is already taken, try peter123 instead"
引用OP:
“如果用户存在,脚本
返回does_user_exist.php
。”1
这可能与您想要的相反。由于
1
可能会被解释为 true
,这告诉插件它“通过”了验证。根据文档,如果用户存在并且您想要触发错误消息,则响应必须是 false
、undefined
、null
或字符串(消息)。
不幸的是,上面的答案有点神秘。
为了让它工作,你需要遵循 JS。
$('#register-form').validate({
rules: {
'first-name': "required",
'surname': "required",
'email': {
required: true,
email: true,
remote: {
url: 'ajax/users/emailInUse',
type: 'post',
data: {
email: function() {
return $('input[name=email]').val();
}
}
}
},
'username': "required",
'password': "required",
'confirm-password': {
equalTo: "input[name=password]"
}
},
messages: {
'first-name': "Please enter your first name",
'surname': "Please enter your surname",
'email': {
required: "Please enter your email address",
email: "Please enter a valid email e.g. [email protected]"
},
'username': "Please enter a username",
'password': "Please enter a password",
'confirm-password': "Passwords do not match"
}
});
您的后端代码应该运行检查电子邮件是否有效,并将结果作为 json 字符串中的消息返回。例如
[
" is already in use..."
]
后端代码示例:
public function emailInUseAction()
{
$this->setJsonResponse();
if($this->request->isAjax())
{
$email = $this->request->getPost('email');
$emailInUse = Users::emailInUse($email);
$message = false;
if($emailInUse)
{
$message = $email . ' is already in use...';
}
return array(
$message
);
}
}
希望这对遇到此问题的人有所帮助!
如果您需要端点以 JSON 形式返回验证结果,而不是像“true”或“false”这样的布尔字符串,您可以使用
dataFilter
来验证响应。
假设您有一个端点来验证用户名是否存在,返回结果为:
{"result": true}
您可以这样检查结果:
$('form').validate({
rules: {
username: {
minlength: 3,
maxlength: 30,
remote: {
url: "/validate/username/",
type: "post",
dataType: "json",
data: {
username: function () {
return $("#id_username").val()
}
},
dataFilter: function (response) {
response = $.parseJSON(response);
return response.result
}
}
},
...
});
返回字符串不会自动显示。如果你想显示它,你需要做这样的事情:
$('#myDiv').html('<p class="error">Sorry, that user already exists.</p>');