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()] );
}
$encoder->encodePassword($user->getUsername(), $user-getPassword());
to this$encoder->encodePassword($user, $user-getPassword());
– teeyo