Symfony2中有2个密码字段不同

时间:2015-07-22 09:01:35

标签: forms symfony types passwords field

在我的UserEditType.php

<?php

namespace HearWeGo\HearWeGoBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use HearWeGo\HearWeGoBundle\Entity\User;

class UserEditType extends AbstractType
{
    protected $user;

    public function __construct(User $user)
    {
        $this->user=$user;
    }

    /**
     * @return mixed
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * @param mixed $user
     */
    public function setUser($user)
    {
        $this->user = $user;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('firstName','text',array('data'=>$this->user->getFirstName()))
            ->add('lastName','text',array('data'=>$this->user->getLastName()))
            ->add('email','email',array('data'=>$this->user->getEmail()))
            ->add('dateOfBirth','date',array(
                'data'=>$this->user->getDateOfBirth(),
                'years' => range(date('Y') -100, date('Y')-5)))
            ->add('phone','text',array('data'=>$this->user->getPhone()))
            ->add('password','repeated',array(
                'type'=>'password',
                'invalid_message'=>'Password fields must match',
                'options'=>array('attr'=>array('class'=>'password-field')),
                'required'=>true,
                'first_options'=>array('label'=>'Password'),
                'second_options'=>array('label'=>'Confirm password'),
            ))
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {

        $resolver->setDefaults(array('data_class'=>"HearWeGo\\HearWeGoBundle\\Entity\\User"));

    }

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



}
?>

profile.html.twig视图中:

    <html>

    <head>
        <title>User Profile</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link href="{{ asset('bundles/hearwegohearwego/css/bootstrap.min.css') }}" rel="stylesheet" type="text/css">
        <link href="{{ asset('bundles/hearwegohearwego/css/profile.css') }}" rel="stylesheet" type="text/css">
        <script src="{{ asset('bundles/hearwegohearwego/js/jquery-2.1.4.min.js') }}"></script>
        <script src="{{ asset('bundles/hearwegohearwego/js/bootstrap.min.js') }}"></script>
    </head>

    <body>
        <div id="toppage">
            <img src="{{ asset('bundles/hearwegohearwego/images/banner.png') }}" style="width:100%">
        </div>
        <div class="container" align="center">
            <img src="{{ asset('bundles/hearwegohearwego/images/personal/profile.png') }}" style="height:40px">
            <div class="col-md-6">
                <img src="{{ asset('bundles/hearwegohearwego/images/personal/avatar.png') }}" style="width:300px;margin-top: 10px">
                {{ form_start(form) }}
                <h4>{{ form.firstName.vars.data }} {{ form.lastName.vars.data }}</h4>
            </div>
            <div class="col-md-6" style="text-align:left">
                <h5>First Name</h5>
                {{ form_widget(form.firstName,{'attr':{'size':'40'}}) }}
                <h5>Second Name</h5>
                {{ form_widget(form.lastName,{'attr':{'size':'40'}}) }}
                <h5>Email</h5>
                {{ form_widget(form.email,{'attr':{'size':'40'}}) }}
                <h5>Phone</h5>
                {{ form_widget(form.phone,{'attr':{'size':'40'}}) }}
                <h5>Date of Birth</h5>
                {{ form_widget(form.dateOfBirth) }}
                <h5>Password</h5>
                {{ form_widget(form.password.first,{'attr':{'size':'40'}}) }}
                {{ form_widget(form.password.second,{'attr':{'size':'40'}}) }}
                <br><br>
                {{ form_widget(form.submit) }}
                <br><br>
                {{ form_end(form) }}
            </div>
        </div>
        <div class="container" align="center">
            <img src="{{ asset('bundles/hearwegohearwego/images/personal/purchase.png') }}" style="height:40px">
        </div>
    </body>

    </html>

在控制器中:

/**
     * @Route("/profile",name="edit_profile")
     */
    public function editProfile(Request $request)
    {
        if (!$this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')){
            return  new Response('Please login');
        }

        $this->denyAccessUnlessGranted('ROLE_USER', null, 'Unable to access this page!');

        $user=$this->get('security.token_storage')->getToken()->getUser();
        $form=$this->createForm(new UserEditType($user),$user,array('method'=>'POST','action'=>$this->generateUrl('edit_profile')));
        $form->add('submit','submit',array(
            'label'=>'',
            'attr'=>array('class'=>'my-custom-button')
        ));
        if ($request->getMethod()=='POST')
        {
            $form->handleRequest($request);
            if ($form->isValid())
            {
                $em=$this->getDoctrine()->getEntityManager();
                $em->persist($user);
                $em->flush();
                return $this->render('@HearWeGoHearWeGo/Default/profile.html.twig',array("form"=>$form->createView()));
            }
        }

        return $this->render('@HearWeGoHearWeGo/Default/profile.html.twig',array("form"=>$form->createView()));
    }

当我搜索时,&#34;重复&#34;字段类型创建两个相同的字段,其值必须匹配。我创建的这个视图是供用户编辑他们的个人资料,他们也可以更改密码。我想使用第一个密码字段键入要更改的密码,第二个用于确认。有什么办法吗?

1 个答案:

答案 0 :(得分:1)

您可以做的是,出于安全原因,在表单中添加一个简单的密码字段。想要修改其密码的用户必须提供旧密码。 然后你添加一个重复的密码字段,这样用户就可以输入他的新密码(出于安全原因,重复的字段,所以用户不会输入错误)。您可以跳过这一点,并且只为新密码添加一个简单的密码字段。

总而言之,您需要在表单中使用两个不同的字段。一个用于旧密码,一个用于新密码(重复或简单,如您所愿)。