[英]$this->Session->setFlash() display an error in cakephp 2.3.7
[英]CakePHP: $this->Session->setFlash only works if redirect is configured
如果有兩個必填字段,我有一個為文檔分配編號的函數。這是控制器代碼:
if( ($data['Msr']['app_requester_id'] == 0 && isset($data['Msr']['app_requester_date']) ||
(empty($data['Msr']['app_requester_date'] && $data['Msr']['app_requester_id'] > 0 )) )) {
$this->Session->setFlash(__('You must sign and date this document before a number can be assigned.'));
$this->redirect(array('action'=>'edit', $id));
}
只要存在$ this-> redirect,它就可以正常工作。 問題是,我不想重定向用戶,因為如果遇到此錯誤情況,那么他們將丟失所有未保存的更改。 但是,如果我刪除$ this-> redirect,它將不起作用,即使一個或兩個必填字段為空白,也會分配該數字。 有沒有一種方法可以使它在沒有重定向的情況下工作?
嘗試驗證數據,然后再儲存並拋出一個異常 ,如果所需的字段為空。
try {
if (($data['Msr']['app_requester_id'] == 0 && isset($data['Msr']['app_requester_date'])
|| (empty($data['Msr']['app_requester_date'] && $data['Msr']['app_requester_id'] > 0 )))
) {
throw new Exception('Exception message');
}
} catch (Exception $e) {
$this->data = array(
'success' => false,
'message' => $e->getMessage()
);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.