Symfony 2 - 更新用户密码

时间:2015-02-21 13:33:06

标签: php forms validation symfony

我已经从用户实体表单类型构建了一个添加用户表单。

此表单的验证在validation.yml中指定并处理。

但是,我知道希望实现编辑用户的功能。

我遇到的问题是更新用户密码字段,因为它们是字段上的验证限制,不能留空。

我想覆盖这个验证一些如何编辑用户或某些方法我可以实现这个逻辑(下面的伪代码示例):

If password is blank
    do not update the password field
else if password is populated
    trigger existing validation constraints as set in validation.yml
endif

我已经对网络进行了高低扫描,似乎找不到适合常规做法的解决方案。

控制器方法:

public function editAction(Request $request, $id){
    $title = "Edit Player";
    $user = $this->getUser();

    //find the user objet by id
    $fetchedUser = $this->getDoctrine()
        ->getRepository('BrsUserBundle:User')
        ->findOneById($id);

    //create form from the found user object
    $form = $this->createForm(new UserType(true), $fetchedUser);

    //set the data from the join on the form select
    $form->get('roles')->setData($fetchedUser->getRole()->current());

    $currentPassword = $fetchedUser->getPassword();

    $form->handleRequest($request);

    // if($first = $request->get('user')['password']['first'] === "" && $second = $request->get('user')['password']['second'] === ""){
    //     $request->request->set('password',$currentPassword);
    // }


    // $form->getData()->setPassword('fdsfdsfdsffdsdfsfdsfdsfdsf');

    // print_r($form->getData()->getPassword());
    // var_dump($form->isEmpty());

    return $this->render('BrsUserBundle:Crud:edit.html.twig', array(
            'user'=>$user,
            'title'=>$title,
            'form'=>$form->createView()
        ));
}

用户表单类型:(正在尝试不同的事情)

    public function buildForm(FormBuilderInterface $builder, array $options)
    {


            $builder
                ->add('username')
                ->add('roles','entity',array(
                    'class' => 'BrsUserBundle:Role',
                    'property' => 'role',
                ))
                ->addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $event){
                    $user = $event->getData();
                    $form = $event->getForm();
                    //var_dump($user['password']['first'] === '');
                    // $user['password']['second'] = "adssadasdsadsadsa";
                    // if($form->getData()->getPassword() && ($user['password']['first'] === '' && $user['password']['second'] === '')){
                    //     unset($user['password']);
                    // }

                    //print_r($user);
                    //$event->setData($user);
                    $form->setData($user);
                    // print_r($form->getData());
                    var_dump($form->isValid());
                })
                ->add('fname')
                ->add('lname')
                ->add('email')
                ->add('mobile')
                ->add('active')
                ->add('mentor')
                ->add('initialized');
 //$builder->add( 'first_name', 'text', [ 'constraints' => new NotBlank( ['message'=>'Please provide your First Name.'] ), 'label' => 'First Name' ] )
                $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
                    $user = $event->getData();
                    $form = $event->getForm();
                    print_r($form->getData()->getPassword());
                    $builder->add('password','repeated',array(
                        'type' => 'password',
                        'invalid_message' => 'The password fields must match.',
                        'options' => array('attr' => array('class' => 'password-field')),
                        'first_options'  => array('label' => 'Password'),
                        'second_options' => array('label' => 'Repeat Password'),
                        'required'=>false,
                    ));
                });

                $builder->add('save','submit');
    }

validation.yml

Brs\UserBundle\Entity\User:
properties:
    username:
        - NotBlank: ~
        - Length:
            min: 5
    fname:
        - NotBlank: ~
        - Length:
            min: 3
    lname:
        - NotBlank: ~
        - Length:
            min: 3
    email:
        - Email:
            message: The email "{{ value }}" is not a valid email address.
            checkMX: true
        - NotBlank: ~
        - Length:
            min: 5
    mobile:
        - NotBlank: ~
    active:
    mentor:
    initialized:
        - Type: \DateTime
    password:
        - NotBlank: ~
        - Length:
            min: 8

非常感谢任何帮助。

亚当

1 个答案:

答案 0 :(得分:1)

在FormType中:

(remove password field before)
$builder->add('temppassword','repeated',[
                        'type' => 'password',
                        'mapped'=>false,
                        'required'=>false
                    ]);

在控制器中:

$tempPassword = $form->get('temppassword')->getData();
/*make your logiq*/
if (yourlogic is good)
{
     $user->setPlainpassword( $tempsPassword );
}