W
W
wolverine7772019-05-14 12:24:32
symfony
wolverine777, 2019-05-14 12:24:32

How to place addFlash - message in the place I need and not by default?

Hi, I need to place a success flash message above one of the tables on my page.
Of course, when I write
$this->addFlash('success', 'Everything is ok');
the message appears by default - at the top of the screen.
But how can I make it appear where I need it, for example, above one of the tables in my twig-a?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bkosun, 2019-05-14
@wolverine777

use Symfony\Component\HttpFoundation\Request;

public function update(Request $request)
{
    // ...

    if ($form->isSubmitted() && $form->isValid()) {
        // do some sort of processing

        $this->addFlash(
            'notice',
            'Your changes were saved!'
        );
        // $this->addFlash() is equivalent to $request->getSession()->getFlashBag()->add()

        return $this->redirectToRoute(...);
    }

    return $this->render(...);
}

{# templates/base.html.twig #}

{# read and display just one flash message type #}
{% for message in app.flashes('notice') %}
    <div class="flash-notice">
        {{ message }}
    </div>
{% endfor %}

{# read and display several types of flash messages #}
{% for label, messages in app.flashes(['success', 'warning']) %}
    {% for message in messages %}
        <div class="flash-{{ label }}">
            {{ message }}
        </div>
    {% endfor %}
{% endfor %}

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

https://symfony.com/doc/master/controller.html#fla...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question