dps43378 2019-03-23 16:23 采纳率: 0%
浏览 30

添加FormError会在从未到达行时导致注销

I have built a FormType to change the users password. When i check whether the oldPassword field matches with the users old password, i add a FormError to this field.

In case the given old Password is wrong, it works and the error is displayed.

But if the given oldPassword is correct, the line where i add my FormError causes a logout, the password is not changed and i get redirected to the login page.

When i comment this line out, changing the password works. (Not depending on the oldPassword field)

The builder from my 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);

My Controller:

/**
     * @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()
        ]);
    }

My Repository: https://gitlab.com/user010101/simple-timelock/tree/master/app

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类
    • ¥15 微带串馈天线阵列每个阵元宽度计算
    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据