我正在尝试通过激活网址激活CakePHP 2
的用户,但我在用户注册页面上收到验证错误。
我已经创建了一个注册操作和视图,我已经定义了这个UserModel
验证规则:
<?php
class User extends AppModel {
public $name = 'User';
public $validate = array (
'username' => array (
'not_empty' => array (
'rule' => 'notEmpty',
'message' => 'Username cannot be empty'
)
),
'password' => array (
'not_empty' => array (
'rule' => 'notEmpty',
'message' => 'Password cannot be empty'
),
'between_chars' => array (
'rule' => array ('between', 5, 40),
'message' => 'Password must be between 5 and 40 chars'
),
'match_password' => array (
'rule' => 'matchPasswords',
'message' => 'Password is different from Password confirm'
)
),
'password_confirm' => array (
'not_empty' => array (
'rule' => 'notEmpty',
'message' => 'Password confirm cannot be empty'
),
'between_chars' => array (
'rule' => array ('between', 5, 40),
'message' => 'Password confirm must be between 5 and 40 chars'
)
),
'email' => array (
'invalid_email' => array (
'rule' => 'email',
'message' => 'Invalid email, try again'
),
'existing_email' => array (
'rule' => 'isUnique',
'message' => 'This email is already registered'
)
),
'activation_key' => array (
'alphanumeric_key' => array(
'allowEmpty' => true,
'rule' => 'alphaNumeric',
'message' => 'Invalid activation key',
'last' => true
)
)
);
public function matchPasswords ($data) {
debug($this->data); // Undefined index: password_confirm [APP/Model/User.php, line 59] points here
if ($data['password'] == $this->data['User']['password_confirm']) {
return true;
}
$this->invalidate('password_confirm', 'Password confirm must be equal to Password field');
return false;
}
public function beforeSave () {
if (isset($this->data['User']['password'])) {
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
}
return true;
}
}
?>
在我使用激活码触及activate
的{{1}}操作之前,一切都运作正常,此处我得到一个UsersController
,其中Undefined index: password_confirm [APP/Model/User.php, line 59]
指向matchPasswords
}} User
验证规则。
Model
为什么我收到错误?
为什么在<?php
App::uses('CakeEmail', 'Network/Email');
class UsersController extends AppController {
public $name = 'Users';
public function index () {
$this->User->recursive = 0;
$this->set('users', $this->User->find('all'));
}
public function view ($id = null) {
if (!$id) {
$this->Session->setFlash('Invalid user');
$this->redirect(array('action'=>'index'));
}
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException('Invalid user');
}
$this->set('user', $this->User->read());
}
public function edit () {
}
public function delete () {
}
public function register () {
// code for user registration
}
public function registration ($sub_action = null) {
}
public function password ($sub_action = null, $code = null) {
if ($sub_action == 'reset' && !empty ($code)) {
$this->render('password_reset_code');
} else if ($sub_action == 'reset' && empty ($code)) {
$this->render('password_reset_mail');
}
}
public function login () {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect ($this->Auth->redirect());
} else {
$this->Session->setFlash('Errore di accesso, email/password sbagliati, ricontrolla i dati inseriti');
}
}
}
public function logout () {
$this->redirect($this->Auth->logout());
}
public function activate ($code = null) {
if (!empty ($code)) {
$user = $this->User->find('first', array('conditions' => array('User.activation_key' => $code)));
if (!empty($user)) {
$user['User']['activation_key'] = null;
$user['User']['active'] = 1;
if ($this->User->save($user)) { // here is where Invalid index error starts
$this->render('activation_successful');
} else {
$this->set('status', 'Salvataggio fallito');
$this->render('activation_fail');
}
// debug($this->User->invalidFields());
// debug($this->User->data);
} else {
$this->set('status', 'Nessun account collegato alla chiave');
$this->render('activation_fail');
}
} else {
$this->set('status', 'Nessuna chiave');
$this->render('activation_fail');
}
}
private function registrationEmail ($account_email, $username, $code) {
// code for email registration
}
}
?>
操作中执行了matchPasswords
?
这些验证规则是否在控制器的每个视图中执行?
答案 0 :(得分:0)
尝试在activate()函数中保存前设置用户模型的ID:
$this->User->id = $user['User']['id'];
或类似的东西。
如果未在save()函数之前设置id,则会尝试创建新的表行。这就是它验证一切的原因。
答案 1 :(得分:0)
尝试更改
if ($data['password'] == $this->data['User']['password_confirm']) {
到
if (isset($this->data['User']['password_confirm']) && $data['password'] == $this->data['User']['password_confirm']) {
在User.php的matchPasswords函数中
在激活操作中,您将获得用户的所有字段,然后保存它们,以便在您保存的数据中将是密码字段。由于密码字段存在,CakePHP正在尝试根据不存在的password_confirm字段对其进行验证。
您应该只验证密码和密码确认字段是否匹配,即当您在表单中有password_confirm时。