symfony - 在 form->handleRequest 处注销

symfony - getting logged out at form->handleRequest

我的 Symfony4 应用程序有问题。

发生了什么事?

我想为当前登录的用户建立一个“更改密码”功能。到目前为止一切都很好。当我提交表单时,一切似乎都运行良好(重定向到正确的页面,显示页面,..)。但是当我想导航到另一个页面时,由于没有身份验证,我被重定向到我的登录页面。在那里我发现,密码也没有更改。

我非常感谢任何形式的帮助!

编辑

无论是否有错误,提交表单时都会发生注销。

控制器

/**
 * @Route("/user/change-password", name="_user_change_password", methods={"GET","POST"})
 * @Template("admin/change_password.html.twig")
 */
public function changePasswordAction(Request $request, UserPasswordEncoderInterface $encoder)
{
    /**
     * @var $user User
     */
    $user = $this->getUser();
    $form = $this->createForm(ChangeOwnPasswordFormType::class, $user);
    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        $oldPassword = $form->get("oldPassword")->getData();
        $checkPass = $encoder->isPasswordValid($user, $oldPassword);
        if(!$checkPass) {
            $this->addFlash("error", "user.wrong_old_password");
            return array(
                "form" => $form->createView()
            );
        }
        $entityManager = $this->getDoctrine()->getManager();

        $newPassword = $form->get("password")->getData();
        $user->setPassword($encoder->encodePassword($user, $newPassword));
        $user->setUpdatedAt(new \DateTime());

        $entityManager->flush();
        $this->addFlash("success", "user.password_changed");
        return $this->redirectToRoute("_user_change_password");
    }
    return array(
        "form" => $form->createView()
    );
}

表单类型

class ChangeOwnPasswordFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('oldPassword', PasswordType::class, array(
                'label' => 'user.old_password',
                'mapped' => false,
                'attr' => array(
                    'autocomplete' => 'current-password',
                ),
            ))
            ->add('password', RepeatedType::class, array(
                'type' => PasswordType::class,
                'first_options' => array(
                    'constraints' => array(
                        new NotBlank([
                            'message' => 'password_reset.password.blank',
                        ]),
                        new Length([
                            'min' => 6,
                            'minMessage' => 'password_reset.password.short',
                            'max' => 4096,
                            'maxMessage' => 'password_reset.password.short',
                        ]),
                    ),
                    'label' => 'user.password'
                ),
                'second_options' => array('label' => 'user.password_confirmation'),
                'invalid_message' => 'user.password_mismatch',
                'options' => array(
                    'attr' => array(
                        'autocomplete' => 'new-password',
                    ),
                )
            ))
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => User::class,
            'validation_groups' => array("Create")
        ));
    }
}

树枝

{% extends "base.html.twig" %}

{% block body %}
    <h1>{{ ("user.change_password.title")|trans }}</h1>
    {{ form_start(form) }}
    {{ form_widget(form) }}

    <button type="submit" class="btn btn-success">
        {{ ("button.save")|trans }}
    </button>

    {{ form_end(form) }}
{% endblock %}

只是 post 如果有人遇到同样的问题,请提供解决方法。就像 Jakumi 提到的,我正在映射用户对象的密码。不知何故,在通过 symfony 验证时,用户被注销了。

有效的方法是从表单中删除用户对象,因此这里有一些更新的片段:

控制器创建表单

$form = $this->createForm(ChangeOwnPasswordFormType::class);
$form->handleRequest($request);

表单类型配置选项

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array());
}