2014-04-01 110 views
1

我有一個重置密碼的功能。 在那裏我有比較密碼和password_confirm場點,我得到一個奇怪的現象:cakephp表單驗證密碼比較

我的形式

echo $this->Form->hidden('tkn', array('value' => $tkn)); 
echo $this->Form->hidden('uid', array('value' => $uid)); 
echo $this->Form->input('password',array('type' => 'password', 'name' => 'data[Appuser][password]')); 
echo $this->Form->input('password_confirm',array('type' => 'password', 'name' => 'data[Appuser][password_confirm]')); 

我的模型驗證:

var $validate = array(
     'password' => array(
      'rule' => array('minLength', '6'), 
      'message' => '{password} minLength 6!' 
     ), 
     'password_confirm' => array(
      'rule' => array('equaltofield','password'), 
      'message' => '{password_confirm} not equal!' 
     ), 
    ); 

function equaltofield($val1, $val2){ 
     return $this->data[$this->alias][key($val1)] == $this->data[$this->alias][$val2]; 
    } 

我的控制器:

if($this->Appuser->save($this->data)){ 

       $this->Session->setFlash(__('Password has been updated')); 

      }else{ 
       debug($this->Appuser->invalidFields()); 
      } 

現在: 當我s ubmit一個空的形式,我得到以下從invalidFields()返回

array(
    'password' => '*****', 
    'password_confirm' => array(
     (int) 0 => '{password_confirm} not equal!', 
     (int) 1 => '{password_confirm} not equal!' 
    ) 
) 

問題1:爲什麼我沒有得到消息,密碼已經不是它的使用MINLENGTH? 問題2:爲什麼我會比較密碼的第二條消息兩次?

當在2個不同的密碼與最小長度打字,我這又得:

array(
    'password_confirm' => array(
     (int) 0 => '{password_confirm} not equal!', 
     (int) 1 => '{password_confirm} not equal!' 
    ) 
) 

當調試($這個 - >數據),我也得到這個(是否有幫助不知)

array(
    'Api' => array(
     'tkn' => '6837d241bf1076c3c55a95abbcfafa04dc19a33c', 
     'uid' => '1' 
    ), 
    'Appuser' => array(
     'password' => '*****', 
     'password_confirm' => 'asdfgh' 
    ) 
) 

關於我上面兩個問題的任何想法? 在此先感謝!

回答

0

試試這個

var $validate = array(
     'password' => array(
      'rule' => array('minLength', '6'), 
      'message' => '{password} minLength 6!' 
     ), 
     'password_confirm' => array(
      'rule' => 'equaltofield', 
      'message' => '{password_confirm} not equal!' 
     ), 
    ); 

function equaltofield($data){ 
     return $this->data[$this->alias]['password'] == $this->data[$this->alias]['password_confirm']; 
    }