1

I create form bundle user in Symfony 3.4

everything works fine .my register form works. and I can register the user .and I can login too. I try to create edit form for member .i cannot edit password member. I read this post but I have not any result ..anyone knows where is my wrong

it is my user entity

/**
     * @Assert\NotBlank()
     * @Assert\Length(max=4096)
     */
    private $plainPassword;

    /**
     * The below length depends on the "algorithm" you use for encoding
     * the password, but this works well with bcrypt.
     *
     * @ORM\Column(type="string", length=64)
     */
    private $password;

 public function getPlainPassword()
    {
        return $this->plainPassword;
    }

    public function setPlainPassword($password)
    {
        $this->plainPassword = $password;
    }
 public function getPassword()
    {
        return $this->password;
    }

    public function setPassword($password)
    {
        $this->password = $password;
    }
  public function eraseCredentials()
    {
    }

UserType

    .
    .
    .

    ->add('oldPlainPassword', \Symfony\Component\Form\Extension\Core\Type\PasswordType::class, array(
    'constraints' => array(
        new \Symfony\Component\Security\Core\Validator\Constraints\UserPassword(),
    ),
    'mapped' => false,
    'required' => true,
    'label' => 'Current Password',
    ))

       ->add('plainPassword', RepeatedType::class, array(
                    'type' => PasswordType::class,
                    'first_options'  => array('label' => 'Password'),
                    'second_options' => array('label' => 'Repeat Password'),
                ))
            ;
        }
    .
    .
    .

my controller

    .
    .
    .
   /**
     * @Route("/admin__/EditUser/{id}" ,name="editUser")
     */
    public function EditUserAction(Request $request, $id)
    {
    .
    .
    .
  if ($request->getMethod() == Request::METHOD_POST){
  $form->handleRequest($request);
    .
    .
    .
    $password = $passwordEncoder->encodePassword($user, $user->getPlainPassword());
           $user->setPassword($password);

   .
   .
   .
   $em->flush();

edit_user.html.twig

 {{ form_row(form.plainPassword.second) }}
    {{ form_row(form.plainPassword.first) }}
    {{ form_row(form.oldPlainPassword) }}
H.shabani
  • 11
  • 2
  • have you flush the data? how are you getting the user? Why you can't update the user password, is there an exception or else?Could you post the rest of the controller code please? – Juan I. Morales Pestana May 31 '18 at 20:31
  • thank you .. I can edit name or family and etc..but only about password I have a problem. I don't know how I can edit plain password user...the code has not any exception..without any error. I can update all fields except the password field – H.shabani May 31 '18 at 20:47
  • what is the intention of 'pw' as paramenter in setpassword function? could you check with a dump or else that you are recieving the updated password as plaintext and post the results ? – Juan I. Morales Pestana May 31 '18 at 21:03
  • When I registered user I used this code-->> $password = $passwordEncoder->encodePassword($User, $User->getPlainPassword('pw')); $User->setPassword($password); i guss maybe for edit i have to do that too ...i use this article for creat register form......https://symfony.com/doc/current/doctrine/registration_form.html – H.shabani May 31 '18 at 21:12
  • try to validate the form after the handle request, like this `$form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $password = $passwordEncoder->encodePassword($user, $user->getPlainPassword()); $user->setPassword($password); $entityManager = $this->getDoctrine()->getManager(); $entityManager->persist($user); $entityManager->flush(); return $this->redirectToRoute('replace_with_some_route'); }`and remove the 'pw' paramenter from the function – Juan I. Morales Pestana May 31 '18 at 21:17
  • thank you so much ..but again I cannot update field password. – H.shabani May 31 '18 at 21:20

0 Answers0