1
votes

I try to make a simple module in order to register. Anytime when I submit the form, this error occur:

Catchable Fatal Error: Argument 1 passed to Symfony\Component\Security\Core\Encoder\UserPasswordEncoder::encodePassword() must implement interface Symfony\Component\Security\Core\User\UserInterface, string given, called in ...

I read this article but is not clear how to implement UserPasswordEncoderInterface.

This is my action method:

public function registerAction(UserPasswordEncoderInterface $encoder, Request $request){
       $user = new User();
        $form = $this->createFormBuilder($user)
                     ->add('username', TextType::class, array('label' => 'Username', 'attr' => array('class' => 'form-control')))
                     ->add('password', PasswordType::class, array('label' => 'Password', 'attr' => array('class' => 'form-control')))
                     ->add('register', SubmitType::class, array('label' => 'Register', 'attr' => array('class' => 'btn btn-default')))
                     ->getForm();

        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()){
         $user->setRoles(array('ROLE_ADMIN')); ///ROLE_USER
         $encoded = $encoder->encodePassword($user->getUsername(), $user-getPassword());
         $user->setPassword($encoded);
            $em = $this->getDoctrine()->getManager();
            $em->persist($user);
            $em->flush();
            return $this->redirectToRoute('login');
        }

       return $this->render('register.html.twig', ['form' => $form-createView()] );
     }
1
Think about what the error message is telling you. The first argument needs to be a $user object, you are passing the user name. Sometimes it helps to look at the actual component source code. You should be able to view the UserPasswordEncoder source code using a control key from inside your IDE.Cerad
try to change this line $encoder->encodePassword($user->getUsername(), $user-getPassword()); to this $encoder->encodePassword($user, $user-getPassword());teeyo

1 Answers

1
votes

Indeed

$encoded = $encoder->encodePassword($user, $user->getPassword());

thanks Cerad!