我正在开发一个小型的cakephp网站。我唯一的问题是,在修改编辑用户并留下空白密码字段后,注册不起作用。我尝试了很多选项,但我的注册仍然无效。这是我的代码:
// User.php - Model
public function beforeSave($options = array()) {
parent::beforeSave($options);
if (!empty($this->data[$this->alias]['pwd'])) {
$PasswordHasher = new SimplePasswordHasher();
$this->data[$this->alias]['password'] = $PasswordHasher->hash($this- >data[$this->alias]['pwd']);
}
return true;
}
// UsersController.php - 控制器(注册部分)
public function register() {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < 10; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
$this->set('password', $randomString);
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->User->save($this->request->data, true, array('username', 'mail', 'firstName', 'lastName', 'pwd', 'pwd_confirmation'))) {
$this->Session->setFlash('Rejestracja przebiegła pomyślnie!');
return $this->redirect(array('controller' => 'pages', 'action' => 'home'));
}
$this->Session->setFlash('Rejestracja nie powiodła się, spróbój jeszcze raz.');
unset($this->request->data['User']['pwd']);
unset($this->request->data['User']['pwd_confirmation']);
}
}
// register.ctp - 使用注册表格查看
echo $this->Form->create('User', array('action'=>'register'));
echo $this->Form->input('username', array('label'=>'Nazwa użytkownika'));
echo $this->Form->input('pwd', array('label'=>'Hasło - należy skopiować', 'type'=>'text', 'value'=>$password, 'disabled' => 'disabled', 'autocomplete'=>'off'));
echo $this->Form->input('pwd_confirmation', array('type'=>'hidden', 'value'=>$password, 'autocomplete'=>'off'));
'label'=>'Potwierź hasło'));
echo $this->Form->input('mail', array('label'=>'Adres e-mail'));
echo $this->Form->input('firstName', array('label'=>'Imię'));
echo $this->Form->input('lastName', array('label'=>'Nazwisko'));
echo $this->Form->end('Zapisz');
我的错误是:Error: SQLSTATE[HY000]: General error: 1364 Field 'password' doesn't have a default value
在某些情况下(我尝试了很多选项来解决我的问题),它只是给出了关于注册失败的flash消息。
答案 0 :(得分:0)
事实证明,无法禁用密码标签。
答案 1 :(得分:0)
而不是:$ this-&gt; set('password',$ randomString); 尝试设置
if ($this->request->is('post') || $this->request->is('put')) {
$this->request->data['User'] = $randomString;
if ($this->User->save($this->request->data, true, array('username', 'mail', 'firstName', 'lastName', 'pwd', 'pwd_confirmation'))) {
$this->Session->setFlash('Rejestracja przebiegła pomyślnie!');
return $this->redirect(array('controller' => 'pages', 'action' => 'home'));
}
$this->Session->setFlash('Rejestracja nie powiodła się, spróbój jeszcze raz.');
unset($this->request->data['User']['pwd']);
unset($this->request->data['User']['pwd_confirmation']);
}