Php Symfony 4中用户密码的自定义表单

Php Symfony 4中用户密码的自定义表单,php,symfony,symfony4,easyadmin,Php,Symfony,Symfony4,Easyadmin,在Easy Admin中,我已经有了一个用户列表/编辑表单。我想添加一个额外的表单来更改任何成员用户的密码。(密码、重复密码、提交) 在自定义表单中,它们被告知是特定于实体的。例如,要创建自定义产品表单,请创建自定义控制器: easy_admin: entities: # ... Product: controller: AppBundle\Controller\ProductController # ... 但是这个解决方案不适合我的问题。我已

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

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

easy_admin:
entities:
    # ...
    Product:
        controller: AppBundle\Controller\ProductController
        # ...
但是这个解决方案不适合我的问题。我已经设置了一个用户表单并使用该表单


我可以设置一个事件侦听器并保存密码,但我一直在添加这个简单的表单

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

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

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 %}
{%extends'base.html.twig%}
{%block title%}更改密码
{%endblock%}
{%块样式表%}
{{parent()}}
{{encore_entry_link_tags('change-password')}
{%endblock%}
{%block body%}
{{parent()}}
{{form_start(changePasswordForm,{'attr':{'class':'form-signin'}}}}
{{form_行(changePasswordForm.oldPassword,{'attr':{'class':'form-control mb-2'}}}}}
{{form_行(changePasswordForm.newPassword.first,{'attr':{'class':'form-control mb-2'}}}}}
{{form_行(changePasswordForm.newPassword.second,{'attr':{'class':'form-control mb-2'}}}}}
更改密码
{{form_end(changePasswordForm)}
{%endblock%}
{%block javascripts%}
{{encore_entry_script_tags('change-password')}
{%endblock%}

我在“显示用户”操作中创建了一个按钮,可将您带到/user/change password,基本上就是这样。

谢谢,帮助了我!
{% 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 %}