我有一個顯示評論表單的動作是評論/添加。如果表單不驗證,我不希望用戶進入評論/添加(該視圖不存在),但我希望他們留在同一頁面,並在那裏看到驗證錯誤。CakePHP重定向後保留驗證錯誤
但是,重定向到$ this-> referer()不起作用 - 驗證錯誤消失,只剩下flash消息。
public function add(){
if (!empty($this->data)){
$this->Comment->create();
if ($this->Comment->save($this->data)){
$this->Session->setFlash('Comment added.','success');
$this->redirect($this->referer());
}else{
$this->Session->setFlash('There was a problem adding your comment. Please try again.','failure');
}
}
}
如何保留驗證錯誤和表單數據重定向?如果這是不可能的,我還能如何解決這個問題?
非常感謝,
威爾
謝謝,它的工作原理。對於任何感興趣的人,我必須將Comment添加到BlogsController的$ uses數組中。然後它就可以開箱即用。 – Will 2011-03-11 09:11:41