我正在尝试通过用户激活页面更改注册用户的状态CakePHP 2
。激活页面包含位于此用户表中的散列字符串:
id username email password active key
1 bill me@domain.com 4u2iu4hk24(hashed str) 0 2ol3p(hashed str)
激活过程适用于这样的 url:
http://url.com/users/activate/2ol3pth3i89txc2zd6tg3
我所做的就是获取散列键,用它搜索注册用户key
,加载它,删除散列键并将active
状态更改为1
. 我在这种Controller
方法中执行此过程:
public function activate ($code = false) {
if (!empty ($code)) {
$this->User->findByActivationKey($code); // magic find instead of $this->User->find('contidions', array('User.activation_key' => $code));
if (!empty($user)) {
$this->User->set(array (
'activation_key' => '0',
'active' => 1
));
if ($this->User->save()) {
$this->render('activation_successful');
} else {
// here is always where I get with this code
$this->render('activation_fail');
}
} else {
$this->render('activation_fail');
}
}
}
一切都很好,但$this->User->save()
不会工作。
使用debug($this->User->invalidFields());
将返回此错误:
app/Controller/UsersController.php (line 64)
Array
(
[password] => Array
(
[0] => Password must contain <span5</span> chars min and <span>20</span> chars max message.
[1] => Password must contain <span5</span> chars min and <span>20</span> chars max message.
)
)
显然,错误来自模型,但为什么password
会提到?
<?php
App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel {
public $name = 'User';
var $validate = array (
'username' => array (
'MustBeCompiled' => array (
'rule' => 'notEmpty',
'message' => 'Error message'
)
),
'password' => array (
'not_empty' => array (
'rule' => 'notEmpty',
'message' => 'Password cannot be empty message.'
),
'between' => array (
'rule' => array ('between', 5, 20),
'message' => 'Password must contain <span5</span> chars min and <span>20</span> chars max message.'
)
),
'email' => array (
'valid_email' => array (
'rule' => 'email',
'message' => 'Mail invalid message.'
),
'existing_email' => array (
'rule' => 'isUnique',
'message' => 'Mail in use message.'
)
)
);
function beforeSave ($options = array()) {
// the way to hash the password
if (!empty ($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
return true;
}
}
}
?>
也许问题可能出在beforeSave()
?我哪里错了?