Symfony 5.4登录表单PRE_SUBMIT与SUBMIT事件失效问题
问题:SecurityLoginType表单扩展的PRE_SUBMIT与SUBMIT事件无法触发
我为SecurityLoginType创建了一个表单扩展,FormEvents::PRE_SET_DATA事件运行正常,能够添加自定义字段,但FormEvents::PRE_SUBMIT和FormEvents::SUBMIT事件却无法触发,请问我遗漏了什么?
<?php declare(strict_types=1); namespace App\Form\Extension; // use Sylius\Bundle\UserBundle\Form\Type\UserLoginType; use Sylius\Bundle\UiBundle\Form\Type\SecurityLoginType; use Symfony\Component\Form\AbstractTypeExtension; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; use Symfony\Component\Form\Extension\Core\Type\CollectionType; // use Symfony\Component\Form\FormTypeExtensionInterface; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; final class SecurityLoginTypeExtension extends AbstractTypeExtension { public function __construct(private ParameterBagInterface $params, private RequestStack $requestStack, private FlashBagInterface $flashBag) { } /** * {@inheritdoc} * * @param array<mixed> $options */ public function buildForm(FormBuilderInterface $builder, array $options): void { $builder->addEventListener(FormEvents::PRE_SET_DATA, [$this, 'onPreSetData']) ->addEventListener(FormEvents::PRE_SUBMIT, [$this, 'onPreSubmit']) ->addEventListener(FormEvents::SUBMIT, [$this, 'onSubmit']); } public function onPreSetData(FormEvent $event): void { $form = $event->getForm(); $form->add('customPlainPassword', HiddenType::class, [ 'required' => true, 'label' => '', ]); } public function onPreSubmit(FormEvent $event): void { // $form = $event->getForm(); // // echo ' this is on submitting the login form...'; echo 'this is from presubmit'; dd($event); exit(); } public function onSubmit(FormEvent $event): void { echo 'on submit event'; dd($event); } /** * {@inheritdoc} */ public static function getExtendedTypes(): iterable { return [SecurityLoginType::class]; } }
更新说明:
最终我通过自定义认证失败时的security.login.failure事件和认证成功时的security.interactive_login事件实现了需求,配置如下:
app.security.interactive_login: class: App\EventListener\LoginListener arguments: - '@router' - '@sylius.context.channel' tags: - { name: kernel.event_listener, event: security.interactive_login, method: interactiveLogin } - { name: kernel.event_listener, event: security.login.failure, method: interactiveLoginFailed}
内容的提问来源于stack exchange,提问作者Manas
相关产品推荐
相关产品推荐

