I m 试图通过一个用户启动网页()改变注册用户的地位 CakePHP 2
。 启用网页上载有一个贴画,位于该用户表中:
id username email password active key
1 bill [email protected] 4u2iu4hk24(hashed str) 0 2ol3p(hashed str)
启动程序与这种程序相似:
http://url.com/users/activate/2ol3pth3i89txc2zd6tg3
All I do is to get the hashed key, search a registered user with this key
, load it, remove the hashed key and change the active
status to 1
.
I do this procedure in this Controller
method:
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(
赢得了t work。
使用<代码>debug($this->User->invalid Fields());,将这一错误退回:
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.
)
)
显然,这一错误来自《示范法》,但为什么提到了<条码>密码/代码>?
<?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;
}
}
}
?>
Maybe the problem could be in beforeSave()
?
Where I m wrong?