Sonata admin stores a flash message after ajax call

  php, sonata-admin, symfony

I have a dependent fields in my configureFormFields method.
enter image description here

I use Symfony form events for that. In my admin class I wrote the the following code:

$formModifierPet = function (FormInterface $form, User $user = null) {
        $pets = null === $user ? [] : $this
            ->getConfigurationPool()
            ->getContainer()
            ->get('doctrine')
            ->getRepository(Pet::class)
            ->findBy([ 'user' => $user ]);

        $form->add('pet', EntityType::class, [
            'class'       => Pet::class,
            'placeholder' => 'Select Pet',
            'attr'        => [
                'class' => 'js-set-pets-user'
            ],
            'choices'     => $pets,
            'multiple'    => true
        ]);
    };

    $formMapper->getFormBuilder()->addEventListener(
        FormEvents::PRE_SET_DATA,
        function (FormEvent $event) use ($formModifierPet) {
            $formModifierPet($event->getForm(), $this->getSubject()->getUser());
        }
    );

    $formMapper->getFormBuilder()->get('user')->addEventListener(
        FormEvents::POST_SUBMIT,
        function (FormEvent $event) use ($formModifierPet) {
            $formModifierPet($event->getForm()->getParent(), $event->getForm()->getData());
        }
    );

And it’s works fine, but when I click update button – sonata shows the following:
enter image description here

I’ve tried to dump the session and it seems that the flash message appears right after the ajax call. Can you please help me with that?

My service.yml:

admin.event:
  class: AdminBundleAdminEventAdmin
  arguments: [~, KKCBundleEntityEvent, AdminBundleControllerAdminEventAdminController]
  tags:
    - { name: sonata.admin, manager_type: orm, label: All Events }
  calls:
    - [ setTemplate, [ edit, SonataAdmin/AdminBundle/EventAdmin/edit.html.twig ]]

Source: Symfony Questions

LEAVE A COMMENT