gpt4 book ai didi

javascript - Jquery 验证远程不工作

转载 作者:行者123 更新时间:2023-12-03 09:33:45 25 4
gpt4 key购买 nike

我正在使用 fiddle 来验证电子邮件或用户名是否唯一,但远程选项不起作用,我不知道为什么。

这是我的js代码

$('.form').validate({
rules: {
email: {
required: true,
email: true,
remote: {
url: "../app/Codecourse/Validation/Validator.php/",
type: "POST",
data: {
email: function() {
return $( "#email" ).val();
}
}
}
},
username: {
required: true,
rangelength: [6,16],
alpha: true,
remote: {
url: "../app/Codecourse/Validation/Validator.php/",
type: "POST",
data: {
username: function() {
return $( "#username" ).val();
}
}
}
},

这是我的 php 验证类,它扩展了 fiddle

class Validator extends Violin {

protected $user;
protected $hash;
protected $auth;

public function __construct(User $user, Hash $hash, $auth = null) {
$this->user = $user;
$this->hash = $hash;
$this->auth = $auth;

$this->addFieldMessages([
'email' => [
'uniqueEmail' => 'That email is already in use.'
],
'username' => [
'uniqueUsername' => 'That username is already in use.'
]
]);

$this->addRuleMessages([
'matchesCurrentPassword' => 'That does not match your current password'
]);
}

public function validate_uniqueEmail($value, $input, $args) {
$user = $this->user->where('email', $value);

if ($this->auth && $this->auth->email === $value) {
return true;
}

return ! (bool) $user->count();
}

public function validate_uniqueUsername($value, $input, $args) {
return ! (bool) $this->user->where('username', $value)->count();
}

public function validate_matchesCurrentPassword($value, $input, $args) {
if ($this->auth && $this->hash->passwordCheck($value, $this->auth->password)) {
return true;
}
return false;
}
}

我在评论中说我可以提交,但显然我什至无法提交表单,即使它不是唯一的电子邮件或用户名

有人有任何建议可以帮助我解决我的问题吗?

最佳答案

对于有效的表单字段,验证调用的响应应该为 true,对于无效的表单字段,验证调用的响应应该为错误消息。因此,例如,函数 validate_uniqueEmail 应该返回错误消息,而不是 false

关于javascript - Jquery 验证远程不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31405694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com