【问题标题】:2 password fields differ in Symfony2Symfony2 中的 2 个密码字段不同
【发布时间】:2015-10-12 01:25:51
【问题描述】:

在我的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()));
    }

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

【问题讨论】:

  • 乍一看,这应该可行。你得到什么错误?
  • 此代码没有出现任何错误。我只想说重复类型用于 2 个具有匹配值的相同字段。我展示了代码,以便我需要一些帮助来修复它,以便我可以使用 2 个不同值的密码字段,而不是相同的值,第一个更改密码,第二个确认更改
  • 好吧抱歉我没听懂。您不能使用重复的字段来执行此操作。在这种类型的字段中,两个值必须匹配。
  • 所以我问有没有办法做到这一点?

标签: forms symfony types passwords field


【解决方案1】:

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

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

【讨论】:

  • 我创建了一个表单类型,它附加了一个实体,我的用户实体只有一个密码字段,我不能添加2个单独的密码字段,因为实体中不存在第二个密码字段
  • 可以,只要加上参数"mapped" => false,像这样-&gt;add('oldPassword', 'password', array("mapped" =&gt; false));别忘了检查这个字段是否和用户的当前密码匹配
猜你喜欢
  • 2012-06-15
  • 2012-03-29
  • 1970-01-01
  • 2013-12-24
  • 1970-01-01
  • 2017-06-22
  • 2012-12-22
  • 1970-01-01
  • 2015-09-01
相关资源
最近更新 更多