chuckedw
chuckedw

Reputation: 751

Symfony 4 flash messages not working after redirectToRoute()

I don't know what is wrong, this should be really simple.

I have this function:

/**
 *
 * @Route("/fin/carteira/list", name="carteira_list")
 */
public function list(Request $request) {
    $repo = $this->getDoctrine()->getRepository(Carteira::class);
    $dados = $repo->findAll();

    return $this->render('financeiro/carteiraList.html.twig', array (
            'dados' => $dados 
    ));
}

And another function to do the 'delete';

/**
 *
 * @Route("/fin/carteira/{id}/delete", name="carteira_delete")
 * @Method("POST")
 *
 */
public function delete(Request $request, Carteira $carteira) {
    if (! $this->isCsrfTokenValid('delete', $request->request->get('token'))) {
        $this->addFlash('error', 'Erro interno do sistema.');
    } else {
        try {
            $em = $this->getDoctrine()->getManager();
            $em->remove($carteira);
            $em->flush();
            $this->addFlash('success', 'post.deleted_successfully');
        } catch ( \Exception $e ) {
            $this->addFlash('error', 'Erro ao deletar carteira.');
        }
    }

    return $this->redirectToRoute('carteira_list');
}

On my twig template:

{% for flashMessage in app.flashes('error') %}

<div class="alert alert-danger alert-dismissible fade show" role="alert">
    <h4 class="alert-heading">Erro</h4>
    <p>{{ flashMessage }}</p>
</div>

{% endfor %}

But it doesnt shows nothing.

Actually, if I remove the return $this->redirectToRoute('carteira_list'); and hits F5 to reload the page, then the div with the errors shows up.

But what am I doing wrong?? Isn't this, with the return $this->redirectToRoute('carteira_list');, the correct way?

Thanks.

Upvotes: 5

Views: 11711

Answers (4)

Koekenbakker28
Koekenbakker28

Reputation: 261

A little late, but I think your twig code only renders messages of type 'error'. So the post deleted message will never be shown because it's of type 'success'.

{% for flashMessage in app.flashes('error') %}

So instead use the following:

        {% for label, messages in app.flashes %}
            {% for message in messages %}
                <div class="flash-{{ label }}">
                    {{ message }}
                </div>
            {% endfor %}
        {% endfor %}

Source: https://symfony.com/doc/current/controller.html#flash-messages

Upvotes: 2

Eissa
Eissa

Reputation: 325

I had a similar problem, and it wasn't from Symfony or even PHP, it was the Google Chrome browser, I ran the same code on other browsers and it worked like expected.

Upvotes: 0

Hugues D
Hugues D

Reputation: 352

Or even shorter for any type of message, as per the docs https://symfony.com/doc/current/controller.html#flash-messages Try this in your Twig template.

      {# display any flash message #}    
      {% for label, messages in app.flashes %}
        {% for message in messages %}
            <div class="alert alert-{{ label }} alert-dismissible fade show">
                {{ message }}
            </div>
        {% endfor %}
      {% endfor %}

Upvotes: 1

Dirk J. Faber
Dirk J. Faber

Reputation: 4691

Try replacing your Twig code with the code below. It should work I think.

{% for label, flashes in app.session.flashbag.all %}
    {% for flash in flashes %}
        {% if ( label == 'success' ) %}
            <div class="alert alert-success alert-dismissible fade show">
                {{ flash }}
            </div>
        {% elseif ( label == 'error' ) %}
            <div class="alert alert-danger alert-dismissible fade show">
                {{ flash }}
            </div>
        {% endif %}
    {% endfor %}
{% endfor %}

Upvotes: 4

Related Questions