如何解决Sonata管理员在Ajax调用后存储Flash消息
我的configureFormFields
方法中有一个从属字段。
我为此使用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());
}
);
我尝试转储会话,并且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
版本的代码覆盖了默认的编辑操作
我希望它可以节省很多时间
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。