Проверка CakePHP не работает для той же модели, другой вид и функция

Я новичок в CakePHP и пытаюсь создать форму смены пароля, но проверка не работает для смены пароля. Есть идеи, почему?

приложение\Контроллер\UsersController.php

App::uses('AppController', 'Controller');

class UsersController extends AppController {
    public $paginate = array(
        'limit' => 4,
        'order' => 'User.id DESC'
    );

    public function admin_userAdd() {
        if ($this->request->is('post')) {
            pr($this->request->data);
            $this->User->create();
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash('The user has been saved');
                return $this->redirect(array('action' => 'userList'));
            }
            $this->Session->setFlash('The user could not be saved. Please, try again.', 'error');
        }
    }

    public function admin_userList(){
        $users = $this->User->find('all');
        $this->set('users', $this->paginate());
    }

    public function admin_userEdit($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->User->save($this->request->data)) {
                //pr($this->request->data); die();
                $this->Session->setFlash(__('The user has been saved'));
                return $this->redirect(array('action' => 'userList'));
            }
            $this->Session->setFlash(
                __('The user could not be saved. Please, try again.')
            );
        } else {
            $this->request->data = $this->User->read(null, $id);
            unset($this->request->data['User']['password']);
        }

    }

    public function admin_passChange($id = null) {
        $this->User->id = $id;
        if (!empty($this->request->data)) {
            $password = $this->request->data['User']['pass1'];
            if ($this->User->saveField('password',$password)) {
                $this->Session->setFlash('Password has been changed.');
                //return $this->redirect(array('action' => 'userEdit'));
            } else {
                $this->Session->setFlash('Password could not be changed.');
            }
        } else {
            $this->request->data = $this->User->read(null, $id);
        }
    }
}       

приложение\Модель\User.php

App::uses('AuthComponent', 'Controller/Component');

class User extends AppModel {

    public $validate = array(
        'username' => array(
            'nonEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => 'A username is required',
                'allowEmpty' => false
            ),
            'between' => array(
                'rule' => array('between', 4, 15),
                'required' => true,
                'message' => 'Usernames must be between 5 to 15 characters'
            ),
            'unique' => array(
                'on'         => 'create',
                'rule'    => array('isUniqueUsername'),
                'message' => 'This username is already in use'
            ),
            'alphaNumericDashUnderscore' => array(
                'rule'    => array('alphaNumericDashUnderscore'),
                'message' => 'Username can only be letters, numbers and underscores'
            ),
        ),
        'email' => array(
            'required' => array(
                'on' => 'create',
                'rule' => array('email', true),
                'message' => 'Please provide a valid email address.'
            ),
            'unique' => array(
                'on'      => 'create',
                'rule'    => array('isUniqueEmail'),
                'message' => 'This email is already in use',
            ),
            'between' => array(
                'rule' => array('between', 6, 60),
                'message' => 'Usernames must be between 6 to 60 characters'
            )
        ),
        'role' => array(
            'valid' => array(
                'rule' => array('inList', array('admin', 'editor', 'accountent')),
                'message' => 'Please enter a valid role',
                'allowEmpty' => false
            )
        ),
        'password' => array(
            'length' => array(
                'rule'      => array('between', 5, 40),
                'message'   => 'Your password must be between 5 and 40 characters.',
            ),
            'required' => array(
                'rule' => 'notEmpty',
                'message' => 'Please enter a password.'
            )
        ),
        'pass1' => array(
            'length' => array(
                'rule'      => array('between', 8, 40),
                'message'   => 'Your password must be between 5 and 40 characters.',
            ),
            'required' => array(
                'rule' => 'notEmpty',
                'message' => 'Please enter a password.'
            )
        )
    );
    public function beforeSave($options = array()) {
        // hash our password
        if (isset($this->data[$this->alias]['password'])) {
            $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
        }

        // if we get a new password, hash it
        if (isset($this->data[$this->alias]['pass1'])) {
            $this->data[$this->alias]['pass1'] = AuthComponent::password($this->data[$this->alias]['pass1']);
        }
        return true;
        // fallback to our parent
        return parent::beforeSave($options);
    }

}

app\View\Users\admin_pass_change.ctp

<?php echo $this->Session->flash(); ?>
<div class="row" style="float:right">
    Hi <?php echo $this->data['User']['username']; ?>
</div>
<div class="row">
    <div class="col-lg-6">
        <?php echo $this->Form->create('User'); ?>
        <?php

        echo $this->Form->input('pass1', array('label' =>'New Password', 'class' => 'form-control'));
        echo $this->Form->input('pass2', array('label' =>'Confirm Password', 'class' => 'form-control'));

        ?>

        <?php echo $this->Form->end(__('Submit', array('class' => 'btn btn-default'))); ?>
    </div>

person subh    schedule 24.12.2015    source источник


Ответы (1)


username включает'required'=>true в массив проверки, но отсутствует в форме смены пароля.

'username' => array(
    'nonEmpty' => array(
        'rule' => array('notEmpty'),
        'message' => 'A username is required',
        'allowEmpty' => false
    ),
    'between' => array( 
        'rule' => array('between', 4, 15), 
        'required' => true, // <----------- REQUIRED
        'message' => 'Usernames must be between 5 to 15 characters'
    ),

Попробуйте установить его на 'required'=>'create'

    'between' => array( 
        'rule' => array('between', 4, 15), 
        'required' => 'create',
        'message' => 'Usernames must be between 5 to 15 characters'
    ),
person Inigo Flores    schedule 24.12.2015