CakePHP AuthComponent散列空字符串会导致notEmpty验证问题

时间:2011-01-27 16:11:40

标签: php validation cakephp cakephp-1.3

我的密码字段设置了notEmpty验证规则。问题是,AuthComponent自动散列字符串。因此,如果密码为空,它将在验证之前变为散列,因此在散列时它将显示为空,但实际的纯文本密码为空。

我能想到的最佳解决方案是使AuthComponent不散列为空字符串。谁能告诉我怎么做?还是一个更好的解决方案?

1 个答案:

答案 0 :(得分:2)

一个想法:您可以在用户模型的beforeValidate回调方法中重置密码,如:

public function beforeValidate() {
    App::import('Core', 'Security'); // not sure whether this is necessary
    if ($this->data['User']['password'] == Security::hash('', null, true)) {
        $this->data['User']['password'] = '';
    }
    return true;
}