0

我正在尝试CakePHP 2通过激活 url 激活用户,但在用户注册页面上出现验证错误。

我创建了一个注册操作和视图,并在其中定义了此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操作,在这里我得到了验证规则的指向。UsersControllerUndefined index: password_confirm [APP/Model/User.php, line 59]matchPasswordsUser 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
    }
}
?>

为什么我得到错误?
为什么matchPasswordsactivateaction中执行?
这些验证规则是否在控制器的每个视图中执行?

4

2 回答 2

0

Try setting the User model's id before the save in the activate() function:

$this->User->id = $user['User']['id'];

or something similar.

When you don't set the id prior to the save() function, it tries to make a new table row. That's why it's validating everything.

于 2012-01-05T22:52:22.473 回答
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 时。

于 2012-01-07T00:07:25.033 回答