tags:

views:

953

answers:

1

In an action, I set some feedback for the user :

    $this->getUser()->setFlash('message', array(
    "type" => "notice",
    "content" => "Well done buddy, you did it."
    ));

    $this->redirect('home/index');

In the view, I just use the following code :

<?php if ($sf_user->hasFlash('message')): $message = $sf_user->getFlash('message') ?>
  <p class="<?php echo $message["type"] ?>" ><?php echo $message["content"]?></p>
<?php endif; ?>

But the message never appear.

I'm using Symfony 1.2, this is why flash var are now binded to the user object.

A: 

Flashes in Symfony are for displaying the response exactly once on a page, i.e. "flash" a message. If the page is refreshed, that flash won't be there anymore.

So assuming the user submitted a form, and the action executed the following code

$this->getUser()->setFlash( 'message', 'Some message here' );

And the view has the following

# Note: getFlash returns "" if flash is not set
echo $sf_user->getFlash('message');

Then the very first request from this user to the view after submitting the form, and thus setting that flash would result in getFlash returning "Some message here" and removing / unsetting the flash. So any request for the same message from that user will now return an empty string.

What this means is, a flash lasts for only one request. It is a good way to display form responses on the same page as the form.

abhinavg
I know that flash are ephemerial variables. I didn't mean it work once, then I reloaded the web page and it didn't work. I meant it worked the expected way once, then whole mecanisme didn't work anymore. Maybe I changed soemthing, but I don't feel like it.
e-satis
I understand the confusion so I removed the sentence to avoid this answser to be repeated.
e-satis