2012-04-16 55 views
2

因此,驗證錯誤顯示在我的添加動作中,但不在我的編輯動作中。下面是我的控制器中的片段:CakePHP:爲什麼不驗證錯誤顯示?

在這裏,我得到驗證錯誤消息的預期:

public function add() { 
     if ($this->request->is('post')) { 
      if ($this->User->save($this->request->data)) { 
       $this->Session->setFlash(__('The user has been saved.')); 
       $this->redirect(array('action' => 'index')); 
      } else { 
       $this->Session->setFlash(__('The user could not be saved. Please try again.')); 
      } 
     } 
     $this->set('clients', $this->User->Client->find('list')); 
    } 

但不是在這裏:

public function edit($id = null) { 
     $this->User->id = $id; 
     if (!$this->User->exists()) { 
      throw new NotFoundException(__('Invalid user')); 
     } 
     if ($this->request->is('post') || $this->request->is('put')) { 
      if ($this->User->save($this->request->data)) { 
       $this->Session->setFlash(__('The user has been saved.')); 
       $this->redirect(array('action' => 'index')); 
      } else { 
       $this->Session->setFlash(__('The user could not be saved. Please, try again.')); 
      } 
     } else { 
      $this->request->data = $this->User->read(null, $id); 
      unset($this->request->data['User']['password']); 
     } 
     $this->set('user', $this->User->read()); 
     $this->set('clients', $this->User->Client->find('list')); 
    } 
+0

錯字:如果($這個 - >用戶 - >保存($這個 - >請求 - >數據/)) - 額外/末 – RichardAtHome 2012-04-16 13:18:51

+0

啊,我的原始代碼中有一條評論,我刪除了SO。 – 2012-04-16 13:21:02

回答

0

是在setFlash(「用戶不能保存」 )消息發射?

什麼是調試的輸出中($這個 - >用戶 - > validationErrors) - 添加它的setFlash後失敗消息

貴後陣列包含需要保存的所有領域?你的$ validate中有一個必填字段,在你的數組中沒有? (Cake會註冊錯誤,但不能顯示它,除非你在表單中有該字段)。或者另一個失敗的驗證規則,但您沒有在表單中顯示該字段?