如何匹配cakephp中不存在于数据库中的字段

时间:2012-07-14 07:37:07

标签: cakephp controllers

如果new_password和confirm_password字段不在数据库中,如何匹配?

嗨......我想知道如何匹配我的字段“new_password”和“confirm_password”,它们不存储在数据库中,它们仅用于更改密码模块中的匹配目的。

我尝试了这个,但它没有奏效:

if($this->data['User']['new_password'] != $this->data['User']['confirm_password'] ) {
  $this->Session->setFlash("New password and Confirm password field do not match");
} else {
  $this->data['User']['password'] = $this->data['User']['new_password'];
  $this->data['User']['id'] = $this->User->id;

  if($this->User->save($this->data)) {
    $this->Session->setFlash("Password updated");
    $this->redirect('/users/login');
  }
}

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

// check for empty value

if( !empty( $this->data['User']['new_password'] ) && !empty( $this->data['User']['confirm_password'] ) ) {

   if( $this->data['User']['new_password'] != $this->data['User']['confirm_password']) {

       $this->Session->setFlash("New password and Confirm password field do not match");

   } else {

       $this->data['User']['password'] = $this->data['User']['new_password'];
       $this->data['User']['id'] = $this->User->id;

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

          $this->Session->setFlash("Password updated");
          $this->redirect('/users/login');

       } else {

          $this->Session->setFlash('Password update fail');

       }
   }

} else {

   $this->Session->setFlash('Enter New password and Confirm password');

}