Symfony2 session-flash with if argument in branch not working

I try to respond to an established session flash, but always get an else path

Symfony 2.1.3

Controller:

$this->get('session')->getFlashBag()->set('contactActionNoticeError', 'Message not sent');

View (tried the "old" and the new style) But I get bla2

{% if app.session.flashbag.has("contactActionNoticeError") or app.session.hasFlash("contactActionNoticeError") %}
    bla1
{% else %}
    bla2
{% endif %}

when showing all flashes with this:

{% for label, flashes in app.session.flashbag.all %}
    {% for flash in flashes %}
        {{ label }} - {{ flash }}
    {% endfor %}
{% endfor %}

I get this:

contactActionNoticeError - Message not sent
+4
source share
3 answers

Get the contents of the flashbag, and then see if it is empty or not:

{% set contactActionNoticeError = app.session.flashbag.get("contactActionNoticeError") %}

{% if (contactActionNoticeError is not empty) %}
    bla1
{% else %}
    bla2
{% endif %}

You can still display errors (code taken from the documentation ):

{% for flashMessage in contactActionNoticeError %}
    <div>
        {{ flashMessage }}
    </div>
{% endfor %}
+3
source

, , -. , , - ( , )

// Instead of 
{% if app.session.flashBag.get('success') is not empty %}
// Use this instead
{% if app.session.flashBag.peek('success') is not empty %}

: Github FlashBag

+2

, FOSUserBundle?

, FOSUserBundle - , .

, , 2.1.3, , 2.0.x:

http://symfony.com/doc/master/components/http_foundation/sessions.html

0

All Articles