Symfony2 - how to clear / edit a flash message
Asked Answered
O

3

8

I am setting a flash message in my controller when rendering a twig template. If there is a post action, I would like to redirect to the same page, but change the flash message.

if ($request->isMethod('POST')) {
    ...
    ...

    $this->get('session')->getFlashBag()->clear(); // Does not work
    $this->get('session')->getFlashBag()->all();   // Does not work

    $request->getSession()->getFlashBag()->set('user-notice', $flash_message2);

    return $this->redirect($request->headers->get('referer'));
}


$this->get('session')->getFlashBag()->set('user-notice', $flash_message1);

return $this->render(....

But the problem is that the displayed flash messages is the $flash_message1, and should be $flash_message2.

When trying to use add instead of set, I can see them both. I tryied to use the Symfony2 clear() and all() functions: http://api.symfony.com/2.3/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.html but nothing changed.

Any idea? Thank you !!!

Ormandy answered 19/11, 2013 at 13:35 Comment(3)
could you please include the method's relevant code completely and maybe add how your attempt with clear() and set() looked?Brooke
problem is that the displayed flash messages is the 1st one. ... what message gets returned if you send a POST request? ... $flash_message or $flash_message 2 ?Brooke
When using the post, flashMessage2 needs to be displayed, but instead of this one, the flashMessage1 is displayed.Ormandy
F
15

Use...

$flashBag = $this->get('session')->getFlashBag();
$flashBag->get('user-notice'); // gets message and clears type
$flashBag->set('user-notice', $flash_message2);

... after your isPost() condition.

Freespoken answered 19/11, 2013 at 13:52 Comment(4)
Ahhh, I know what's the problem. When I am using redirect, I am redirecting to the same page, so even if I set the flashMessage2, an the end the flashMessage1 will be set because I am redirecting to the same page. Any idea on how to fix that ?Ormandy
Thanks, but this did not fixed teh problem. The $flashBag->has('user-notice') is allwaays false.Ormandy
have you cleared your cache and your browser cache ? Updated my answer - just use get() to clear the message. It has to be unset afterwards ... see FlashBag Line 97.Brooke
Yes, both caches are cleared. About your new answer, the problem is how to know if I have to set the 1st or 2nd message. In both cases, I am comming from an redirect, so not from post. Should I use a simple render instead of redirect ?Ormandy
P
16

To clear all flash messages use the following code:

$this->get('session')->getFlashBag()->clear();
Papistry answered 11/11, 2014 at 11:50 Comment(2)
Why was this downvoted ? An explanation would help people like me, reading this, understand what's going on.Heavyarmed
Upvoted, there's absolutely no reason this should be downvoted. The only thing to note is that this clears the ENTIRE flashbag and not a specific one like 'user-notice'.Portly
F
15

Use...

$flashBag = $this->get('session')->getFlashBag();
$flashBag->get('user-notice'); // gets message and clears type
$flashBag->set('user-notice', $flash_message2);

... after your isPost() condition.

Freespoken answered 19/11, 2013 at 13:52 Comment(4)
Ahhh, I know what's the problem. When I am using redirect, I am redirecting to the same page, so even if I set the flashMessage2, an the end the flashMessage1 will be set because I am redirecting to the same page. Any idea on how to fix that ?Ormandy
Thanks, but this did not fixed teh problem. The $flashBag->has('user-notice') is allwaays false.Ormandy
have you cleared your cache and your browser cache ? Updated my answer - just use get() to clear the message. It has to be unset afterwards ... see FlashBag Line 97.Brooke
Yes, both caches are cleared. About your new answer, the problem is how to know if I have to set the 1st or 2nd message. In both cases, I am comming from an redirect, so not from post. Should I use a simple render instead of redirect ?Ormandy
I
2

One easy way to delete all flash messages is as follows:

// clear all messages from FlashBag
$flashBag = $this->get('session')->getFlashBag();
foreach ($flashBag->keys() as $type) {
    $flashBag->set($type, array());
}

This works just fine in Symfony 2.4, and probably all other recent versions.

Itemize answered 4/4, 2014 at 7:19 Comment(2)
That is not easy! The easy way is this: $this->get('session')->getFlashBag()->clear();Strow
@Trix There used to be an issue with exactly what you suggested two years ago. Have another look at the question: Milos is asking what to do when it does not work that way.Itemize

© 2022 - 2024 — McMap. All rights reserved.