【发布时间】:2011-02-14 12:08:07
【问题描述】:
假设我有一个用户注册并且我正在使用 Auth 组件(当然允许 /user/register)。
问题是如果我需要在模型中设置一个 minLength 验证规则,它不起作用,因为 Auth 组件对密码进行哈希处理,因此它总是超过我的 minlength 密码,即使它是空白的,它也会通过。
我该如何解决这个问题?提前致谢!
【问题讨论】:
-
这很奇怪。 minLength 验证规则对我来说非常适合 Auth 组件。我对密码字段有两个验证规则:minLength 和 notEmpty。
-
@bancer:这绝对不是我的经验。你用的是什么版本的蛋糕?你的验证码是什么样的?
-
@mikermcneil:这是蛋糕 1.2。 $validate 数组的一部分:
'password' => array( 'minLength' => array( 'rule' => array('minLength', '8') ), 'notEmpty' => array( 'rule' => 'notEmpty', 'required' => true ) ), 'confirm_password' => array( 'minLength' => array( 'rule' => array('minLength', '8'), 'required' => true ), 'notEmpty' => array( 'rule' => 'notEmpty' ), 'comparePasswords' => array( 'rule' => '_comparePasswords' // Protected function below ), ). -
@mikermcneil:密码比较方法:
protected function _comparePasswords(){ $hashedConfirmPassword = Security::hash($this->data['User']['confirm_password'], null, true); if($this->data['User']['password'] == $hashedConfirmPassword){ return true; }else{ return false; } }
标签: authentication cakephp validation