2012-11-01 91 views
-2

我可以這樣做來搜索表中的電子郵件。在查詢數據庫之前使用模型進行驗證

// controller method 
public function forgot_password() { 
    if ($this->Session->read('Auth.User')) { 
     $this->redirect(array('action' => 'add')); 
    } else { 
     if ($this->request->is('post') || $this->request->is('put')) { 
      $user = $this->User->findByEmail($this->request->data('User.email')); 
      if ($user) { 
        $this->request->data['User']['id'] = $user['User']['id']; 
        $this->request->data['User']['random_string'] = $this->String->random(); 
        unset($this->request->data['User']['email']); 
        $this->User->save($this->request->data); 
        // $this->_sendEmail($user); 
        $this->Session->setFlash(__('Instructions has been sent to your email'), 'flash'); 
        $this->redirect(array('action' => 'forgot_password')); 

      } else { 
       // passed! do stuff 
      } 
     } 
    } 
} 


// validate in the model 
public $validate = array(
    'email' => array(
     'required' => array(
      'rule' => array('notEmpty'), 
      'message' => 'An Email is required' 
     ), 
     'email' => array(
      'rule' => array('email'), 
      'message' => 'Email is invalid' 
     ), 
     'isUnique' => array(
      'rule' => array('isUnique'), 
      'message' => 'Email is already in use' 
     ) 
    ), 
    'password' => array(
     'required' => array(
      'rule' => array('notEmpty'), 
      'message' => 'A password is required' 
     ) 
    ), 
    'role' => array(
     'valid' => array(
      'rule' => array('inList', array('admin', 'author')), 
      'message' => 'Please enter a valid role', 
      'allowEmpty' => false 
     ) 
    ) 
); 

上面的代碼工作正常。

我只是想在驗證數據庫之前驗證電子郵件是否是有效的電子郵件或爲空。我想出了下面的一個。我遇到的問題是使用$this->request->data來設置用戶。無論何時驗證,它都通過isUnique規則運行並失敗。

public function forgot_password() { 
    if ($this->Session->read('Auth.User')) { 
     $this->redirect(array('action' => 'add')); 
    } else { 
     if ($this->request->is('post') || $this->request->is('put')) { 
      $this->User->set($this->request->data); 
      if ($this->User->validates()) { 
       $user = $this->User->findByEmail($this->request->data('User.email')); 
       if ($user) { 
        $this->request->data['User']['id'] = $user['User']['id']; 
        $this->request->data['User']['random_string'] = $this->String->random(); 
        unset($this->request->data['User']['email']); 
        $this->User->save($this->request->data); 
        // $this->_sendEmail($user); 
        $this->Session->setFlash(__('Instructions has been sent to your email'), 'flash'); 
        $this->redirect(array('action' => 'forgot_password')); 
       } 
      } 
     } 
    } 
} 

已經有人做過類似的,以我想要什麼解決辦法?

+0

倘使你寫了一個簡短的解釋(而不僅僅是代碼)你做了什麼/希望做什麼 – Dave

+0

我想我已經解釋了很多,我已經驗證,並且由於驗證規則'isUnique'而失敗,我想驗證模型不查詢數據庫 –

回答

1

你可以從你的控制器做到這一點。

App::uses('Validation', 'Utility'); 
if (Validation::email($this->request->data('User.email'))) { 
    ... 
} 
+0

+1感謝您對此信息的瞭解 –

+0

這個問題不會增加驗證錯誤 –

0

我同意@Dave,你不是很清楚你想做什麼。首先你訪問請求數據應該是$ this-> request-> data ['User'] ['email'];如果您以標準格式助手格式發佈數據。

我很確定如果值爲空或電子郵件規則失敗,那麼在保存調用中查詢數據庫之前,模型驗證將失敗。由於notEmpty是第一條規則,因此在查詢數據庫之前將通過驗證實用程序對其進行檢查。如果失敗,那麼該字段將被跳過,並且不會調用db(只要'last'不等於false。同樣適用於電子郵件規則,因爲cake爲該規則使用正則表達式模式。您可以遵循事件鏈通過檢查保存功能Model.php。

如果不是空的,郵件規則通過,最終你將不得不查詢數據庫發現,如果該地址已經存在。

+0

我不能同意你答案中較爲清楚的部分,但我同意你對它的陳述,而不是查詢。沒有睡覺:p –

+0

好的。我可以繞過'isUnique'上的驗證嗎?因爲我在'isUnique'上得到錯誤。 –

+0

是的,有一個部分用於動態更改蛋糕文檔中的規則。但我認爲你的方法是這裏的問題,而不是蛋糕處理驗證的方式。 http://book.cakephp.org/2.0/en/models/data-validation.html –