Jquery验证远程无法正常工作

时间:2015-07-14 11:46:53

标签: javascript php jquery validation

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

这是我的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验证课,它扩展了小提琴

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;
    }
}

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

有没有人有任何建议可以帮我解决问题?

1 个答案:

答案 0 :(得分:1)

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