From a57ead18e27fb862299e53d6e32a43f668c682f7 Mon Sep 17 00:00:00 2001 From: Kevin Frantz Date: Wed, 12 Sep 2018 20:00:35 +0200 Subject: [PATCH] Added new registration form --- .../src/Controller/RegistrationController.php | 61 +++++++++++++++++ application/src/Controller/UserController.php | 66 ------------------- .../Controller/UserControllerInterface.php | 18 ----- application/src/Form/UserType.php | 33 ++++++++++ application/templates/user/register.html.twig | 1 + 5 files changed, 95 insertions(+), 84 deletions(-) create mode 100644 application/src/Controller/RegistrationController.php delete mode 100644 application/src/Controller/UserController.php delete mode 100644 application/src/Controller/UserControllerInterface.php create mode 100644 application/src/Form/UserType.php diff --git a/application/src/Controller/RegistrationController.php b/application/src/Controller/RegistrationController.php new file mode 100644 index 0000000..cabcf7c --- /dev/null +++ b/application/src/Controller/RegistrationController.php @@ -0,0 +1,61 @@ +user = new User(); + $form = $this->createForm(UserType::class, $this->user); + $form->handleRequest($request); + if ($form->isSubmitted() && $form->isValid()) { + $this->encodePassword($passwordEncoder); + $this->saveUser($translator); + return $this->redirectToRoute('login'); + } + return $this->render("user/register.html.twig", array( + 'form' => $form->createView() + )); + } + + public function encodePassword(UserPasswordEncoderInterface $passwordEncoder): void + { + $password = $passwordEncoder->encodePassword($this->user, $this->user->getPlainPassword()); + $this->user->setPassword($password); + } + + private function saveUser(TranslatorInterface $translator): void + { + $entityManager = $this->getDoctrine()->getManager(); + $entityManager->persist($this->user); + try { + $entityManager->flush(); + $this->addFlash('success', $translator->trans('User "%username%" created!',['%username%'=>$this->user->getUsername()])); + } catch (\Exception $exception) { + $this->addFlash('danger', $translator->trans('User "%username%" could not be created!',['%username%'=>$this->user->getUsername()])); + } + } +} + diff --git a/application/src/Controller/UserController.php b/application/src/Controller/UserController.php deleted file mode 100644 index f561877..0000000 --- a/application/src/Controller/UserController.php +++ /dev/null @@ -1,66 +0,0 @@ -render("user/login.html.twig"); - } - - /** - * - * @Route("/user/register", name="user_register") - */ - public function register(Request $request,UserPasswordEncoderInterface $encoder): Response - { - $user = new User(); - $form = $this->createFormBuilder($user) - ->add('username', TextType::class) - ->add('password', PasswordType::class) - ->add('save', SubmitType::class, [ - 'label' => 'register' - ]) - ->getForm(); - $form->handleRequest($request); - if ($form->isSubmitted() && $form->isValid()) { - $user = $form->getData(); - $encoded = $encoder->encodePassword($user, $request->get('password')); - $user->setPassword($encoded); - $entityManager = $this->getDoctrine()->getManager(); - $entityManager->persist($user); - try { - $entityManager->flush(); - $this->addFlash('success', 'User created!'); - } catch (\Exception $exception) { - $this->addFlash('danger', 'User could not be created!'); - $this->addFlash('info', $exception->getMessage()); - } - - } - return $this->render("user/register.html.twig", [ - 'form' => $form->createView() - ]); - } -} \ No newline at end of file diff --git a/application/src/Controller/UserControllerInterface.php b/application/src/Controller/UserControllerInterface.php deleted file mode 100644 index 8393a4f..0000000 --- a/application/src/Controller/UserControllerInterface.php +++ /dev/null @@ -1,18 +0,0 @@ -add('username', TextType::class) + ->add('plainPassword', RepeatedType::class, array( + 'type' => PasswordType::class, + 'first_options' => array('label' => 'Password'), + 'second_options' => array('label' => 'Repeat Password'), + )) + ; + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults(array( + 'data_class' => User::class, + )); + } +} \ No newline at end of file diff --git a/application/templates/user/register.html.twig b/application/templates/user/register.html.twig index 3b8a51c..fdb8f2a 100644 --- a/application/templates/user/register.html.twig +++ b/application/templates/user/register.html.twig @@ -6,5 +6,6 @@

{% trans %}register{% endtrans %}

{{ form_start(form) }} {{ form_widget(form) }} + {{ form_end(form) }} {% endblock %}