createForm(RegistrationFormType::class, $user); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { // encode the plain password $user->setPassword( $userPasswordHasher->hashPassword( $user, $form->get('plainPassword')->getData() ) ); $user->setRoles(['ROLE_USER']); $entityManager->persist($user); $entityManager->flush(); // do anything else you need here, like send an email return $security->login($user, 'form_login', 'main'); } return $this->render('registration/register.html.twig', [ 'registrationForm' => $form, ]); } }