Jonathan Scheiber

EasyAdmin bundle: flash message after save

Read time: 3 minutes 10 comments
EasyAdmin bundle: flash message after save

By default, when creating content, EasyAdmin displays no message after creating, updating or removing entities. Let's see how to manage that :)

You can see an example in the article overview image.

The goal of this article will be to automate these flash messages with as little code as possible.

Some explanations before starting

The code has been tested with PHP 8.0, Symfony 5.2 and EasyAdmin bundle 3.2.8. It is based on the following facts:




namespace App\Listener;

use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityDeletedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Translation\TranslatableMessage;

final class EasyAdminListener implements EventSubscriberInterface
    public function __construct(private SessionInterface $session)

    public static function getSubscribedEvents(): array
        return [
            AfterEntityPersistedEvent::class => ['flashMessageAfterPersist'],
            AfterEntityUpdatedEvent::class => ['flashMessageAfterUpdate'],
            AfterEntityDeletedEvent::class => ['flashMessageAfterDelete'],

    public function flashMessageAfterPersist(AfterEntityPersistedEvent $event): void
        $this->session->getFlashBag()->add('success', new TranslatableMessage('content_admin.flash_message.create', [
            '%name%' => (string) $event->getEntityInstance(),
        ], 'admin'));

    public function flashMessageAfterUpdate(AfterEntityUpdatedEvent $event): void
        $this->session->getFlashBag()->add('success', new TranslatableMessage('content_admin.flash_message.update', [
            '%name%' => (string) $event->getEntityInstance(),
        ], 'admin'));

    public function flashMessageAfterDelete(AfterEntityDeletedEvent $event): void
        $this->session->getFlashBag()->add('success', new TranslatableMessage('content_admin.flash_message.delete', [
            '%name%' => (string) $event->getEntityInstance(),
        ], 'admin'));
# Translations, put in translations/admin.en.yaml
        create: Content "%name%" has been created.
        update: Content "%name%" has been updated.
        delete: Content "%name%" has been removed.

How does it work?

The listener will listen to the three EasyAdmin events warning that a Doctrine entity has been created, updated or deleted. Each of these events has access to the impacted entity via the getEntityInstance() method available on the event.

From there, a flash message is saved in session, passing as a parameter of the flash message the value returned by the __toString() method of the entity.

As soon as autoconfigure is activated in your project, it's over! You have your beautiful flash messages that appear when you save an entity.

Et voilà! :) If you have any questions, comments, suggestions... Feel free to post a comment below :)


Posted by Robin Bastiaan on 25/04/2021 at 13:32.
Elegant solution I will use for sure. Thanks for sharing!
Posted by wietsedc on 26/04/2021 at 09:57.
thanks! details like this make the user experience so much better, I implemented it straight away.
Posted by ced on 08/05/2021 at 16:12.

J'ai une erreur concernant e constructeur
public function __construct(private SessionInterface $session)

private me donne une erreur
Posted by jmsche on 09/05/2021 at 18:02.
Bonjour ced, si tu pouvais donner l'erreur ça pourrait être intéressant pour t'aider :)
Posted by Robin Bastiaan on 10/05/2021 at 10:08.
@ced, please let me help you although I only speak English. I suspect you are not running PHP version 8. A new way of writing a constructor was introduced in PHP8 and is called "constructor property promotion". Please try the following code instead:

private SessionInterface $session;

public function __construct(SessionInterface $session)
$this->session = $session;
Posted by ced on 24/05/2021 at 23:17.
Merci @Robin Bastiaan pour ton indication. J'utilise un switch checkbox et l'ors de la modification sur on ou off je ne reçois pas de message flash car je ne change pas de page.Comment faire pour résoudre ce problème et affiché directement le message?
Posted by DooM on 27/05/2021 at 14:00.
I tried this with php 7.4.9, used the tip from @Robin Bastiaan and ended with this error:
Expected to find class "App\EventSubscriber\EasyAdminListener" in file "$$$$\src\EventSubscriber\EasyAdminListener.php" while importing services from resource "../src/", but it was not found! Check the namespace prefix used with the resource in $$$$\src\../config/services.yaml (which is being imported from "$$$$\src\Kernel.php").
I'm kind of beginner with symfony5
Posted by jmsche on 27/05/2021 at 14:07.
Hi DooM, could you please create a Gist with the code of your listener? Also, make sure the namespace+classname match the filepath (case sensitive).
Posted by DooM on 27/05/2021 at 14:56.
Hi jmsche, it's just the same as your. I managed to "solve" the path problem, no more error, but still no flash message displayed. I guess i should take more time reading doc...
Posted by jmsche on 27/05/2021 at 14:56.
Feel free to contact me on Symfony's Slack by DM :)

Add a comment