src/Controller/RegistrationController.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\RegistrationFormType;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  10. use Symfony\Component\Form\FormError;
  11. class RegistrationController extends AbstractController
  12. {
  13.     /**
  14.      * @Route("/register", name="app_register")
  15.      */
  16.     public function register(Request $requestUserPasswordEncoderInterface $passwordEncoder): Response
  17.     {
  18.         $user = new User();
  19.         $form $this->createForm(RegistrationFormType::class, $user);
  20.         $form->handleRequest($request);
  21.         if($form->isSubmitted()) {
  22.             $answer = (int)$form->get('question')->getData();
  23.             if($answer !== 48) {
  24.                 $form->addError(new FormError('Felaktigt svar på säkerhetsfråga'));
  25.             }
  26.         }
  27.         if ($form->isSubmitted() && $form->isValid()) {
  28.             // encode the plain password
  29.             $user->setPassword(
  30.                 $passwordEncoder->encodePassword(
  31.                     $user,
  32.                     $form->get('plainPassword')->getData()
  33.                 )
  34.             );
  35.             $user->setRoles(['ROLE_USER']);
  36.             $entityManager $this->getDoctrine()->getManager();
  37.             $entityManager->persist($user);
  38.             $entityManager->flush();
  39.             return $this->redirectToRoute('app_register_complete');
  40.         }
  41.         return $this->render('registration/register.html.twig', [
  42.             'registrationForm' => $form->createView(),
  43.             'errorCount' => count($form->getErrors(true))
  44.         ]);
  45.     }
  46.     /**
  47.      * @Route("/register/complete", name="app_register_complete")
  48.      */
  49.     public function complete() {
  50.         return $this->render('registration/complete.html.twig');
  51.     }
  52. }