Is it possible to show flash messages in Symfony 2 without redirect? Or editing core files in another possible solution on google groups?
//Symfony\Component\HttpFoundation\Session
public function setFlash($name, $value, $persist = true)
{
if (false === $this->started) {
$this->start();
}
$this->flashes[$name] = $value;
if($persist) {
unset($this->oldFlashes[$name]);
}
else {
$this->oldFlashes[$name] = $value;
}
}
UPDATE
Oh actually I noticed if I just used a forward, flash messages will show, but it will still show on next request
Why using flashes if you don't want them to be persisted until next request ?
Couldn't you find other ways to display feedback like template parameters ?
If not, you could add this in your templates (according you are displaying flashes like below):
{% if app.session.hasFlash('notice') %}
<div class="flash-notice">
{{ app.session.flash('notice') }}
{{ app.session.removeFlash('notice') }}
</div>
{% endif %}
So that any template that displays these flashes before redirection will remove them from session before returning the response. I think it's the last better solution.
I will speak in general terms, because you want something that is not a ready standard.
At this time Symfony flash messages appear after the request, they are this way, and to preserve it, you need to find an alternative, usign an ajax call.
You need to call an action script using an ajax request, serializing the data of a form, getting the message to return and then display it the way it server you better.
I used http://jquery.bassistance.de/message/demo/ along with this sample jquery call in some projects, works good:
$.post("/product/saveAjax", { $("#product").serialize() },
function(data){
$().message(data.message);
}, "json");
The return from saving an item with ajax is different, in this case i´m encoding the data using JSON, so, if you want to perfom another actions you can inject more variables in the JSON array, manipulating them inside function(data) { ... }
Hope that helps
Flash messages have been created in order to live through the redirection you usually do after a form submission success. That's why they live for 2 requests.
If you want to manage soem user notifications in your app, make a controller managing a notification TWIG block in your layout. This block can embed flash messages AND these notifications you handle with the controller. You'll have to set them in your session, remove them in your NotificationController and it's alright...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With