Sonata 管理员在 ajax 次通话后存储了一条即时消息

Sonata admin stores a flash message after ajax call

我的 configureFormFields 方法中有一个依赖字段。

我为此使用 Symfony 表单事件。在我的管理员 class 中,我写了以下代码:

$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());
        }
    );

它工作正常,但是当我单击更新按钮时 - sonata 显示以下内容:

我已尝试转储会话,闪现消息似乎在 ajax 调用后立即出现。你能帮我吗?

我的service.yml:

admin.event:
  class: AdminBundle\Admin\EventAdmin
  arguments: [~, KKCBundle\Entity\Event, AdminBundle\Controller\Admin\EventAdminController]
  tags:
    - { name: sonata.admin, manager_type: orm, label: All Events }
  calls:
    - [ setTemplate, [ edit, SonataAdmin/AdminBundle/EventAdmin/edit.html.twig ]]

问题已解决。这是 sonataAdmin 版本的差异。例如。我的奏鸣曲版本是: "sonata-project/admin-bundle": "3.57.0" 我可以在其中看到以下几行:

vendor/sonata-project/admin-bundle/src/Controller/CRUDController.php (editAction)


if (!$isFormValid) {
    if ($this->isXmlHttpRequest() && null !== ($response = $this->handleXmlHttpRequestErrorResponse($request, $form))) {
        return $response;
    }

    $this->addFlash(
        'sonata_flash_error',
            $this->trans(
                'flash_edit_error',
                    ['%name%' => $this->escapeHtml($this->admin->toString($existingObject))],
                    'SonataAdminBundle'
                )
            );
} elseif ($this->isPreviewRequested()) {
            // enable the preview template if the form was valid and preview was requested
      $templateKey = 'preview';
            $this->admin->getShow();
        }
    }

但在另一个版本的奏鸣曲3.54.1中还有另一个代码:

if (!$isFormValid) {
            if (!$this->isXmlHttpRequest()) {
                $this->addFlash(
                    'sonata_flash_error',
                    $this->trans(
                        'flash_edit_error',
                        ['%name%' => $this->escapeHtml($this->admin->toString($existingObject))],
                        'SonataAdminBundle'
                    )
                );
            }
        } elseif ($this->isPreviewRequested()) {
            // enable the preview template if the form was valid and preview was requested
            $templateKey = 'preview';
            $this->admin->getShow();
        }

因此,为了避免出现不必要的错误消息,我用版本 3.54.1

中的代码覆盖了默认编辑操作

我希望它可以节省很多人的时间