How tp show flash message on CGridView#delete?

Hi, first of all,I’m pretty new to Yii, but really impressed with what I can do with the framework!

I have to model objects, Customer and Project, and a project has a Customer as it’s owner. There’s no hard foreign key constraint, but one in SQL comments. Now, when I want to delete a Customer from the admin view (a generated controller with actions), I check whether the customer is referenced by one or more projects. If so, I don’t delete and set a flash message. But the message doesn’t appear… why not? It has to do with AJAX, but how? When this action is invoked from the ‘update’ page (no AJAX), this works. Here is my action method:

/**


 * Deletes a particular model.


 * If deletion is successful, the browser will be redirected to the 'index' page.


 */


public function actionDelete()


{


	if(Yii::app()->request->isPostRequest)


	{


		// Check if this customer is not referenced as a project owner or intermediate


		$c = $this->loadModel();


		$numRefs = Project::model()->count(


			'ownerId=:ownerId OR intermediateId=:intId',


			array(':ownerId'=>$c->id,':intId'=>$c->id)


		);


		if ($numRefs == 0) 


		{


			// we only allow deletion via POST request


			$c->delete();


		}


		else 


		{


			Yii::app()->user->setFlash('message',"This customer is referenced from one or more projects, either as the customer or as the intermediate.");	


		}


		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser


		if(!isset($_POST['ajax']))


		{


			$this->redirect(array('index'));


		}


	}


	else


		throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');


}

and my view (partly):

<?php if(Yii::app()->user->hasFlash(‘message’)):?>

&lt;div class=&quot;error&quot;&gt;


    &lt;?php echo Yii::app()-&gt;user-&gt;getFlash('message'); ?&gt;


&lt;/div&gt;

<?php endif; ?>

Regards,

just throw new exception like this :


throw new CHttpException(400,'Sorry you can not delete this!');

in your code do this :




if ($numRefs == 0)

{

// we only allow deletion via POST request

$c->delete();

}

else

{

throw new CHttpException(400,'This customer is referenced from one or more projects, either as the customer or as the intermediate.');

}



Than the grid will get error response and will alert your message.

Is not a simple stuff.

Take a look at this thread, it can give you some explaination.