添加FormError导致永远无法到达行时注销

时间:2019-03-23 16:23:51

标签: php symfony symfony-forms symfony4

我建立了一个FormType来更改用户密码。 当我检查oldPassword字段是否与用户的旧密码匹配时,我向该字段添加FormError。

如果给定的旧密码有误,它将起作用并显示错误。

但是,如果给定的oldPassword是正确的,则我添加FormError的行将导致注销,密码不会更改,并且我会重定向到登录页面。

当我注释掉该行时,更改密码有效。 (不取决于oldPassword字段)

我的UserPasswordType中的构建器:

$builder
            ->add('passwordOld', PasswordType::class, [
                'mapped' => false,
                'label' => 'Aktuelles Passwort'
            ])
            ->add('password', RepeatedType::class, [
                'type' => PasswordType::class,
                'invalid_message' => 'Die Passwortfelder müssen übereinstimmen.',
                'required' => true,
                'first_options' => ['label' => 'Neues Passwort'],
                'second_options' => ['label' => 'Wiederholen'],
            ])
            ->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $formEvent) {
                /** @var User $user */
                $user = $formEvent->getData();
                if (!$this->userPasswordEncoder->isPasswordValid($user,
                    $formEvent->getForm()->get('passwordOld')->getData())) {
                    $formEvent->getForm()->get('passwordOld')->addError(new FormError('Passwort ist falsch.'));
                }
                $user->setPassword($this->userPasswordEncoder->encodePassword($user, $user->getPassword()));
            })
            ->add('submit', SubmitType::class);

我的控制器:

/**
     * @Route("/settings", name="app_settings")
     * @param Request $request
     * @param ObjectManager $objectManager
     * @param UserInterface $user
     * @return Response
     */
    public function __invoke(Request $request, ObjectManager $objectManager, UserInterface $user): Response
    {
        $form = $this->createForm(UserPasswordType::class, $user);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $objectManager->flush();
        }

        return $this->render('user/settings.html.twig', [
            'form' => $form->createView()
        ]);
    }

我的存储库: https://gitlab.com/user010101/simple-timelock/tree/master/app

0 个答案:

没有答案