Validation, flash and go back

  • I am on admin page and want to update an item;

  • On controller, my validation realizes that there's a problem.

I want to set a flash message and show it on the page that generated the request. In this case is simples: I render the admin page. However:

  • I loose the page control. The pagination returns to the first page;

  • The request can come from different places (by the show view, or another linting view).

Do I have some way to obtain the page that generated the request and render it?

Hello Romanoza!

It is not a problem in my Controller. I am not using Ajax requests.

I junt want, in my Controller, to identify a validation error, se a flash message, and render the PREVIOUS page.

For example:

in admin page, I click on delete link. But I cannot delete for any reason and I want to send a message to the user in the SAME ADMIN page.

But, the delete command can come from the 'show' page. Then, I want to render SHOW page with the message. An so on…

Any idea?

I dont use this in my application, but only want show you.

in admin page:

in controller:

About "Go Back", you can consult the last url using this: $_SERVER['HTTP_REFERER']

I actually know about flash message, but thanks for you atention :)

Yes, one solution is use $_SERVER['HTTP_REFERER']

I just though that maybe Yii has some way to control this.

Paste in your page ADMIN or other VIEW page

in your linkButton "Update"

in your Controller action UPDATE, you can validate the items, if OK, you can return to the page before with this:

Great armando! This should work, and it’s so Yii-stylish :D

However, the future versions of Yii could implement something like a 'Yii::app()->Controller->urlBefore' variable.


I also agree, who knows in a near future.  ;D

Valeu brother! É nós do Rio  :D

This is not that easy to implement. And it will make your code more complicated. Take a look at this discussion:


Really not that easy, I know.

Just dreaming with this on future versions -

I am trying to use refresh() method to do this.

In the doc is written:

"Refreshes the current page. The effect of this method call is the same as user pressing the refresh button on the browser (without post data)"

So, I've written my code as below:

public function actionCancel($id=null)




   if ($cad_oficio->isCancelled()){			

	Yii::app()->user->setFlash('success',"Ofício is already cancelled!");				




My intention is: Whetever the request comes, if my model is cancelled, I will refresh the original page with my flash information.

This works for my 'admin' page, but not for 'show' page. In the last, i get an 'invalid request' (probably an infinite loop).

How really refresh() works? Why it doesn't work for show? Is it refresh() able to do what I am wanting to do?

my show method:

public function actionShow()


		if(isset($_POST['command'], $_POST['id']))


			if ($_POST['command']==='cancel')








processAdminCommand() method:

protected function processAdminCommand()


		if(isset($_POST['command'], $_POST['id']))


			if ($_POST['command']==='delete')



			if ($_POST['command']==='cancel')



			// reload the current page to avoid duplicated delete actions




paste in your page view and do your change

<?php if (Yii::app()->user->hasFlash('success')) echo "<div class='success'>" . Yii::app()->user->getFlash('success') . "</div>"; ?>

<?php if (Yii::app()->user->hasFlash('fail')) echo "<div class='fail'>" . Yii::app()->user->getFlash('fail') . "</div>"; ?>

you can create css style for success and fail (note the div tag)

public function actionShow()


      if(isset($_POST['command'], $_POST['id']))


         if ($_POST['command']==='cancel')



          Yii::app()->user->setFlash('success',"Ofício is already cancelled!");   





I have no problemas about the flash message, it works perfectly.

My problems is that i’m trying to do the last part of the subject (go back) with the refresh() method and it is not working on show() method :(

Ric, around here I do something like this:



            // we only allow deletion via POST request


            $this->redirect(array('update' , 'id'=>$_POST['eid'] , 'activeTab'=>$_POST['activeTab'] , 'status'=>$_POST['status'] , 'which'=>$_POST['which'] ));

        } else {

            throw new CHttpException(400,'Invalid request, are you crazy?');


instead of the refresh(), which gives me the same error as yours…

it's like breaking a rock with my hands, but works fine for me…haha

hope it helps, regards!

Yes scoob, that's it.

It would be nice if we could use refresh() as said in the docs.

totally agreed!!