如果空白(symfony2形式)如何使表单忽略密码字段

时间:2012-08-14 21:50:14

标签: php forms symfony

我的后端有一个简单的用户管理器,我希望能够在不设置新密码/每次重复旧密码的情况下编辑用户。

现在,如果我在编辑用户时将密码字段留空,symfony2会抱怨必须输入密码,当然我在注册新用户时需要此功能,但是当我编辑它们时,我想要如果没有填写密码框,则忽略密码框。

这是如何完成的?

8 个答案:

答案 0 :(得分:26)

对于其他人的参考,我是这样做的。

我的formType:

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

    $builder
        ->add('username', 'text', array('label' => 'Servernamn '))            
        ->add('plainPassword', 'repeated', array('type' => 'password', 'first_name' => 'Lösenord för server ', 'second_name' => 'Upprepa lösenord för server',));

    $builder-> addValidator(new CallbackValidator(function(FormInterface $form){
      $username = $form->get('username')->getData();
        if (empty($username)) {
          $form['username']->addError(new FormError("Du måste ange ett namn för servern"));
        }
    }));

}

我的updateAction:

public function updateServerAction($id)
{
    $em = $this->getDoctrine()->getEntityManager();

    $entity = $em->getRepository('BizTVUserBundle:User')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Container entity.');
    }

    $originalPassword = $entity->getPassword(); 

    $editForm   = $this->createForm(new editServerType(), $entity);

    $request = $this->getRequest();

    $editForm->bindRequest($request);

    if ($editForm->isValid()) {

        $plainPassword = $editForm->get('plainPassword')->getData();
        if (!empty($plainPassword))  {  
            //encode the password   
            $encoder = $this->container->get('security.encoder_factory')->getEncoder($entity); //get encoder for hashing pwd later
            $tempPassword = $encoder->encodePassword($entity->getPassword(), $entity->getSalt()); 
            $entity->setPassword($tempPassword);                
        }
        else {
            $entity->setPassword($originalPassword);
        }

        $em->persist($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('Server'));
    }

因此,应该设置更新我的用户密码,否则保留原始密码。

答案 1 :(得分:14)

在您的密码属性的实体设置器上,使其如下所示:

/**
 * Set password
 *
 * @param string $password
 * @return User
 */
public function setPassword($password)
{
    if (!is_null($password)) {
        $this->password = $password;
    }

    return $this;
}

诀窍是检查传递的参数是否为空,只检查它是否为空。

答案 2 :(得分:4)

如果您需要在$ options数组中添加必需选项。例如:

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

    $builder
        ->add('username', 'text', array('label' => 'Servernamn '))            
        ->add('plainPassword', 'repeated', array(
                  'type' => 'password', 
                  'first_name' => 'password', 
                  'second_name' => 'repeat_password', 
                  'required' => false,
            ));
}

答案 3 :(得分:2)

我所做的是按原样离开UserType,只删除控制器中的密码字段:

/**
 * @Route("/users/edit/{username}", name="user_edit")
 */
public function editAction(Request $request, User $user)
{
    $form = $this->createForm(UserType::class, $user);

    // Remove the password field when editing the user
    // Otherwise it must be entered each time the user is edited
    // We can change the password via a special edit-user-password page
    $form->remove('password');

    $form->handleRequest($request);

    if ($form->isValid()) {
        // ...
    }
}

这样你就可以拥有一个UserType,可以在editAction和newAction中重用。

请务必在$form->remove('password');之前添加行$form->handleRequest($request);

答案 4 :(得分:1)

有一篇文章提供了许多选项来实现您的请求,来自Symfony2食谱,特别是下面的部分为我工作:

Customizing your Form based on the underlying Data

以下是实施:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    // ...
    $builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
        $product = $event->getData();
        $form = $event->getForm();

        // check if the Product object is "new"
        // If no data is passed to the form, the data is "null".
        // This should be considered a new "Product"
        if (!$product || null === $product->getId()) {
            $form->add('name', 'text');
        }
    });
}

答案 5 :(得分:0)

对于验证,使用2个不同的验证组,并通过可调用函数设置正确的验证组。

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add(
        'oldPassword',
        PasswordType::class,
        [
            'constraints' => new UserPassword([
                'groups' => 'profile_password',
            ]),
            'mapped' => false,
            'required' => false,
        ]
    );

    $builder->add(
        'plainPassword',
        PasswordType::class,
        [
            'constraints' => new NotBlank([
                'groups' => 'profile_password',
            ]),
            'mapped' => false,
            'required' => false,
        ]
    );
}

/**
 * {@inheritdoc}
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(
        [
            'validation_groups' => function (FormInterface $form) {
                $newPassword = $form->get('plainPassword')->getData();
                $oldPassword = $form->get('oldPassword')->getData();
                if ($oldPassword || $newPassword) {
                    return ['profile', 'profile_password'];
                } else {
                    return ['profile'];
                }
            },
        ]
    );
}

答案 6 :(得分:0)

Symfony 5方法:

在控制器类中:

/**
 * @Route("/user/{id}/edit")
 */
public function updateUser(
    $id, // See 'Route' annot., needed to `find()` User, adjust to your needs
    User $user,
    Request $request,
    UserPasswordEncoderInterface $passwordEncoder,
    UserRepository $userRepository
): Response {
    $form = $this->createForm(UserFormType::class, $user);

    // if password field is required by default, we have to `add()` it again
    // with `required` set to `false`
    $form->add( 
        'plainPassword',
        PasswordType::class,
        [
            // do not use Constraint NotBlank()
            'required' => false,
            'mapped' => false
        ]
    );

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $entity = $userRepository->find($id); // as above, adjust to your needs

        if (!$entity) {
            throw $this->createNotFoundException('User does not exist!');
        }

        $originalPassword = $entity->getPassword();

        /** @var User $user */
        $user = $form->getData();

        // keep original password if new one isn't provided
        if (!empty($form['plainPassword']->getData())) {
            $user->setPassword(
                $passwordEncoder->encodePassword(
                    $user,
                    $form['plainPassword']->getData()
                )
            );
        } else {
            // set old password
            $user->setPassword($originalPassword);
        }

        $em = $this->getDoctrine()->getManager();
        $em->persist($user);
        $em->flush();

        return new Response('User updated!');
    }

    return $this->render(
        'admin/user_edit.html.twig',
        [
            'requestForm' => $form->createView(),
        ]
    );
}

答案 7 :(得分:-3)

你可以用

做点什么
if (empty($password)) {
     //edit the password
}

尝试将其修复到表单的PHP中。