2013-09-27 87 views
2

我有一些Cakephp 2驗證問題。CakePHP 2 - 驗證密碼字段

我想驗證編輯窗體上的幾個字段。其中一些是密碼並確認密碼字段。

我想驗證兩者,只有它們提供。如果它們是空的,我不會更改密碼,但是如果用戶在它們上面寫入,我想驗證它們是否有minLength或密碼匹配。

代碼:

'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 
      ), 

我不知道如果我有在控制器上定義編輯一些規則()函數或者它應該是足夠了,但我的代碼是行不通的。

謝謝!

編輯:(控制器代碼)

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')); ?> 

+0

怎麼樣'valdidate_passwords'功能 –

+0

提示:閱讀[工作與 - 密碼功能於CakePHP的(http://www.dereuromark.de/2011/08/25/working-with-passwords- in-cakephp /) – mark

+0

@mark我之前嘗試過您的解決方案,但它並不符合我的需要。您的解決方案讓我將密碼字段留空,但不驗證它們是否提供。我認爲我做錯了 – Luis

回答

6

作出這樣

'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'] 
} 
+0

我需要讓用戶將pwd和pwd_repeat字段留空,所以密碼不會更改,但如果用戶提供了任何內容,那麼我想驗證這些字段 – Luis

+0

,您可以通過allowEmpty將其設置爲true。你能告訴我你被擊中的地方嗎? –

+0

With allowEmpty我解決了用戶可能會留下空密碼(不變的密碼),但如果他們不是空的(用戶想更改密碼),我正在努力驗證這些字段。我不知道爲什麼驗證規則在這種情況下不起作用。 – Luis