Sonata管理员在Ajax调用后存储Flash消息

问题描述

我的configureFormFields方法中有一个从属字段。

enter image description here

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

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

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

enter image description here

我尝试转储会话,并且Flash消息似乎在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','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版本的代码覆盖了默认的编辑操作

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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...