How Can I Show Flash Message After Ajax Request ?

[size="2"]Anybody know how can I show the flash message in after ajax request was executed ?

I’m using standard Gii-generated code for CRUD operations. When I create or update data in CGridView I see my Flash message. But when data deleted there is now flash, but Yii::app()->user->setFlash(‘success’, ‘My message’) is put message in session.

my actionDelete content is


	public function actionDelete($id)

	{


		$result = $this->loadModel($id)->delete();

        if ($result)

            Yii::app()->user->setFlash('success', 'Data was deleted');

        else

            Yii::app()->user->setFlash('error', 'Error was occurred');


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

        {

            echo Yii::app()->user->getFlash('success');

        } else {

            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array(index));

        }}

[/size]

Hi LIAL,

CGridView’s delete function works via ajax, so it is impossible to show a flash message … at least in the normal way.

One easy solution is something like this:




public function actionDelete($id)

{

	$result = $this->loadModel($id)->delete();


	// for ajax request

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

	{

		if ($result)

		{

			echo "Data has been deleted.";

			Yii::app()->end();

		}

		else

		{

			throw new CHttpException(500, "\nFailed to delete the data.");

		}

	}

	// for normal post request

	else

	{

		if ($result)

		{

			Yii::app()->user->setFlash('success', 'Data has been deleted.');

		}

		else

		{

			Yii::app()->user->setFlash('error', 'Failed to delete the data.');

		}

		$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array(index));

	}

}



By throwing an exception, we can notify the ajax caller about the failure. It will pop up an alert box to show the error.

The drawback of this solution is that you’ll get no message when the deletion has been successfully completed (although the user will know the deletion was successful because the grid will be updated ;) ).

If you do want to show an alert box even when the deletion was successful, you can make use of the ‘afterDelete’ property of CButtonColumn.




// in the controller

	...

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

	{

		if ($result)

		{

			echo "Data has been deleted.";

		}

		else

		{

			echo "Failed to delete the data.";

		}

		Yii::app()->end();

	}

	...


// in the view script


	...

	array(

		'class' => 'CButtonColumn',

		'afterDelete' => "function(link,success,data){ if (success) alert(data); } ",

	),

	...



Check the reference manual for CButtonColumn::afterDelete.

http://www.yiiframework.com/doc/api/1.1/CButtonColumn#afterDelete-detail

Thanks a lot, soft ark.

Vary sad that it is impossible to show standard flash messages.

May be you also know how can I change modal dialog box when I click delete icon (actionDelete will fire) and I have no rights to delete the item (accessControl filter works) I get modal dialog box with error. I would like to make this box with the style of my app, not standard YII style.

I see.

Maybe you can write your own version of ‘afterDelete’ function in which you may pop up your own CJuiDialog, or dynamically construct the contents of the flash message div (that is not originally made ready by Gii, but you may insert it somewhere in the view as it is in create/update views).

One thing comes to my mind.

After the deletion, CGridView will be updated by another ajax call. Normally the page content other than the grid will remain the same in this process. But you can update other elements together with the grid by specifying ‘ajaxUpdate’ property of CGridView.

http://www.yiiframework.com/doc/api/1.1/CGridView#ajaxUpdate-detail




$this->widget('zii.widgets.grid.CGridView', array(

	'id' => 'data-grid',

	'ajaxUpdate' => 'flash-msgs',

	...


...

<div id="flash-msgs">

<?php if(Yii::app()->user->hasFlash("success")): ?>

<div class="flash-success">

<?php echo Yii::app()->user->getFlash("success"); ?>

</div>

<?php elseif(Yii::app()->user->hasFlash("error")): ?>

<div class="flash-error">

<?php echo Yii::app()->user->getFlash("error"); ?>

</div>

<?php endif; ?>

</div>



This will update flash message div together with the grid content in ajax updating of the grid.

And in the controller, you can set the flash message.




public function actionDelete($id)

{

	$result = $this->loadModel($id)->delete();

	if ($result)

	{

		Yii::app()->user->setFlash('success', 'Data has been deleted');

	}

	else

	{

		Yii::app()->user->setFlash('error', 'Failed to delete the data');

	}

	if (!Yii::app()->request->isAjaxRequest)

	{

		$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array(index));

	}

}



The flash message will be rendered the next time the grid will be updated.

Umm, it’s not tested at all. I’m not sure it will work or not. And I guess it won’t work for CHttpException (when the error is caused by access control).

But I hope it’s worth a try.