0

在 Easy Admin 中,我已经有一个用户列表/编辑表单。我想添加一个额外的表单来更改任何成员用户的密码。(密码,重复密码,提交)

文档中,自定义表单被告知是特定于实体的。例如,要创建自定义产品表单,请创建自定义控制器:

easy_admin:
entities:
    # ...
    Product:
        controller: AppBundle\Controller\ProductController
        # ...

但是这个解决方案不适合我的问题。我已经设置了一个用户表单并使用该表单。

我可以设置一个事件侦听器并管理保存密码,但我坚持添加这个简单的表单。

4

1 回答 1

3

首先,您需要一个控制器来处理与您的用户关联的请求(如果您还没有,请创建一个)

    /**
     * @Route("/user/change-password", name="change_password")
     */
    public function changePassword(Request $request, UserPasswordEncoderInterface $passwordEncoder)
    {

        $changePasswordModel = new ChangePassword();
        $form = $this->createForm(ChangePasswordType::class, $changePasswordModel);

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $entityManager = $this->getDoctrine()->getManager();

            $user = $entityManager->find(User::class, $this->getUser()->getId());
            $user->setPassword(
                $passwordEncoder->encodePassword(
                    $user,
                    $form->get('newPassword')->getData()
                )
            );

            $entityManager->persist($user);
            $entityManager->flush();

            return $this->redirect('/?entity=User&action=show&id='. $this->getUser()->getId());
        }

        return $this->render('admin/theme/changePassword/change_password.html.twig', array(
            'changePasswordForm' => $form->createView(),
        ));        
    }

然后我创建了一个如下所示的表单类型:

class ChangePasswordType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('oldPassword', PasswordType::class, [
                        'required' => true,
                        'label' => 'Type your current password',
                        ])
                ->add('newPassword', RepeatedType::class, [
                        'type' => PasswordType::class,
                        'invalid_message' => 'Passwords do not match.',
                        'first_options'  => ['label' => 'Type your new password'],
                        'second_options' => ['label' => 'Retype your new password']    
                 ]);                 
    }

    public function setDefaultOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => ChangePassword::class,
        ));
    }

    public function getName()
    {
        return 'change_passwd';
    }
}

我创建了一个带有自定义验证的表单模型,您可以随意编辑它

class ChangePassword
{
    /**
     * @SecurityAssert\UserPassword(
     *     message = "Wrong value for your current password!"
     * )
     */
    public $oldPassword;

    /**
     * @Assert\Length(
     *     min = 6,
     *     minMessage = "Password must be at least 6 characters long!"
     * )
     */
    public $newPassword;
}

最后,用这样的东西扩展你的基础树枝

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

{% block title %}Change password
{% endblock %}

{% block stylesheets %}
    {{ parent() }}
    {{ encore_entry_link_tags('change-password') }}
{% endblock %}

{% block body %}
    {{ parent() }}
    <body id="{% block body_id %}{% endblock %}">
        <div class="container-fluid h-100">
            <div class="row justify-content-center align-items-center h-100">
                <div class="col col-sm-8 col-md-8 col-lg-6 col-xl-4">
                    {{ form_start(changePasswordForm, {'attr':{'class':'form-signin'}}) }}
                    {{ form_row(changePasswordForm.oldPassword, {'attr': {'class':'form-control mb-2'} }) }}
                    {{ form_row(changePasswordForm.newPassword.first, {'attr': {'class':'form-control mb-2'} }) }}
                    {{ form_row(changePasswordForm.newPassword.second, {'attr': {'class':'form-control mb-2'} }) }}
                    <button class="btn btn-dark btn-lg btn-block mt-3" type="submit">Change password</button>
                    {{ form_end(changePasswordForm) }}
                </div>
            </div>
        </div>
    </body>
{% endblock %}

{% block javascripts %}
    {{ encore_entry_script_tags('change-password') }}
{% endblock %}

我在我的显示用户操作上创建了一个按钮,将您带到 /user/change-password ,差不多就是这样。

于 2019-09-13T12:09:47.840 回答