CakePHP 2 – Проверка полей пароля

У меня есть некоторые проблемы с проверкой Cakephp 2.

Я пытаюсь проверить несколько полей в форме редактирования. Некоторые из них являются паролем и подтверждают пароль.

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

Код:

'pwd' => array( 'length' => array( 'rule' => array('between', 8, 40), 'message' => 'Your password must be between 8 and 40 characters.', 'allowEmpty' => true ), ), 'pwd_repeat' => array( 'length' => array( 'rule' => array('between', 8, 40), 'message' => 'Your password must be between 8 and 40 characters.', 'allowEmpty' => true ), 'compare' => array( 'rule' => array('validate_passwords'), 'message' => 'The passwords you entered do not match.', 'allowEmpty' => true ), 

Я не знаю, нужно ли мне определять некоторые правила в функции edit () в контроллере, или этого должно быть достаточно, но мой код не работает.

Благодаря!

Изменить: (Код контроллера)

 public function edit($id = null) { if (!$this->User->exists($id)) { throw new NotFoundException(__('Usuario incorrecto')); } if ($this->request->is('post') || $this->request->is('put')) { if ($this->User->save($this->request->data)) { $this->Session->setFlash(__('El usuario ha sido actualizado.')); return $this->redirect(array('action' => 'index')); } else { $this->Session->setFlash(__('El usuario no ha podido actualizarse. Por favor, inténtelo de nuevo.')); } unset($this->request->data['User']['pwd']); unset($this->request->data['User']['pwd_repeat']); } else { $options = array('conditions' => array('User.' . $this->User->primaryKey => $id)); $this->request->data = $this->User->find('first', $options); } $roles = $this->User->Role->find('list'); $this->set(compact('roles')); } не public function edit($id = null) { if (!$this->User->exists($id)) { throw new NotFoundException(__('Usuario incorrecto')); } if ($this->request->is('post') || $this->request->is('put')) { if ($this->User->save($this->request->data)) { $this->Session->setFlash(__('El usuario ha sido actualizado.')); return $this->redirect(array('action' => 'index')); } else { $this->Session->setFlash(__('El usuario no ha podido actualizarse. Por favor, inténtelo de nuevo.')); } unset($this->request->data['User']['pwd']); unset($this->request->data['User']['pwd_repeat']); } else { $options = array('conditions' => array('User.' . $this->User->primaryKey => $id)); $this->request->data = $this->User->find('first', $options); } $roles = $this->User->Role->find('list'); $this->set(compact('roles')); } не public function edit($id = null) { if (!$this->User->exists($id)) { throw new NotFoundException(__('Usuario incorrecto')); } if ($this->request->is('post') || $this->request->is('put')) { if ($this->User->save($this->request->data)) { $this->Session->setFlash(__('El usuario ha sido actualizado.')); return $this->redirect(array('action' => 'index')); } else { $this->Session->setFlash(__('El usuario no ha podido actualizarse. Por favor, inténtelo de nuevo.')); } unset($this->request->data['User']['pwd']); unset($this->request->data['User']['pwd_repeat']); } else { $options = array('conditions' => array('User.' . $this->User->primaryKey => $id)); $this->request->data = $this->User->find('first', $options); } $roles = $this->User->Role->find('list'); $this->set(compact('roles')); } 

(Просмотр кода)

 <div id="contenedor" class="users form"> <?php echo $this->Form->create('User', array('name' => 'form')); ?> <fieldset> <legend><?php echo __('Editar Usuario'); ?></legend> <?php echo $this->Form->input('id'); echo $this->Form->input('username', array('label' => __('Usuario'))); echo $this->Form->input('pwd', array('label' => __('Contraseña'), 'type' => 'password', 'name' => 'pass', 'onKeyUp' => 'habilita()', 'value' => '')); echo $this->Form->input('pwd_repeat', array('label' => __('Repite Contraseña'), 'type' => 'password', 'name' => 'rpass', 'disabled' => 'disabled')); echo $this->Form->input('firstname', array('label' => __('Nombre'))); echo $this->Form->input('lastname', array('label' => __('Apellidos'))); echo $this->Form->input('telephone', array('label' => __('Teléfono'))); echo $this->Form->input('email', array('label' => __('Email'))); echo $this->Form->input('role_id', array('label' => __('Rol'))); ?> </fieldset> <?php echo $this->Form->end(__('Aceptar')); ?> 

Related of "CakePHP 2 – Проверка полей пароля"

Выполните правила проверки правильности

 'pwd' => array( 'length' => array( 'rule' => array('between', 8, 40), 'message' => 'Your password must be between 8 and 40 characters.', ), ), 'pwd_repeat' => array( 'length' => array( 'rule' => array('between', 8, 40), 'message' => 'Your password must be between 8 and 40 characters.', ), 'compare' => array( 'rule' => array('validate_passwords'), 'message' => 'The passwords you entered do not match.', ) ) 

И ваша функция validate_passwords должна быть такой.

 public function validate_passwords() { return $this->data[$this->alias]['pwd'] === $this->data[$this->alias]['pwd_repeat'] }