Hi, I am a newbie at Yii framework and I have a question today about displaying custom error message.
First, my current app is baked with the Gii CRUD generator. When deleting a company record, I want to ignore the cases where there is existing job records relate to it and display a message to inform user.
Thus I manually do the checking in onBeforeDelete() then stop the operation and using this in my controller:
public function actionDelete($id)
{
if(Yii::app()->request->isPostRequest)
{
// we only allow deletion via POST request
$result = $this->loadModel($id)->delete();
if(!$result)
{
Yii::app()->user->setFlash('deleteError','This company can not be deleted because there are existing jobs.');
$this->redirect(array('view','id'=>$id));
}
else
{
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
}
}
else
throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
}
It work fine in view page and display the user flash message, but in admin page with zii CGridView and AJAX delete, I can’t figure out how to display the error message.
Is there an easy way around this? Or is there a function to fire custom errors from model & controller easily?
Any help will be greatly appreciated.