Advertisement
Guest User

Untitled

a guest
Sep 25th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. <?php
  2. namespace AppController;
  3.  
  4. use SymfonyBundleFrameworkBundleControllerController;
  5. use AppFormUserType;
  6. use AppEntityUser;
  7. // and more use ...
  8.  
  9. class RegisterController extends Controller
  10. {
  11. /**
  12. * @Route("/{_locale}/register/", name="register", requirements={"_locale"="%app.locales%"})
  13. *
  14. * @param Request $request
  15. * @param UserPasswordEncoderInterface $encoder
  16. *
  17. * @return SymfonyComponentHttpFoundationResponse
  18. */
  19. public function index(Request $request, UserPasswordEncoderInterface $encoder)
  20. {
  21. $isValid = true;
  22. $doAccountLoginValue = 2;
  23. $isUserLoggedIn = $this->isGranted('IS_AUTHENTICATED_FULLY');
  24. $accountForm = $this->getAccountForm();
  25. $accountForm->handleRequest($request);
  26.  
  27. // check if the user wants to login
  28. if ($accountForm->isSubmitted() &&
  29. $accountForm->isValid() &&
  30. intval($accountForm->get('login')->getData()) === $doAccountLoginValue &&
  31. !$isUserLoggedIn
  32. ) {
  33. if ($this->userLogin($request, $encoder)) {
  34. $isUserLoggedIn = true;
  35. } else {
  36. $this->addFlash(
  37. 'error',
  38. 'Login incorrect. Invalid email or password.'
  39. );
  40. }
  41. }
  42. // create the user form. When the user is logged in pass the user object to the UserType
  43. if ($this->getUser()) {
  44. $user = $this->getUser();
  45. } else {
  46. $user = new User();
  47. }
  48. $form = $this->createForm(UserType::class, $user, [
  49. 'action' => $this->generateUrl('register'),
  50. ]);
  51.  
  52. if ($isUserLoggedIn) {
  53. // validate manually when the user is logged in
  54. $form->submit([], false);
  55. $violationList = $this->get('validator')->validate($form);
  56. $isValid = (empty($violationList)) ? true : false;
  57. $this->addViolationMessagesToForm($violationList, $form);
  58. } else {
  59. // not logged in, so handle the request with its POST data
  60. $form->handleRequest($request);
  61.  
  62. if ($form->isSubmitted()) {
  63. $isValid = $form->isValid();
  64. }
  65. }
  66.  
  67. return $this->render('register/index.html.twig', [
  68. 'form' => $form->createView(),
  69. 'accountForm' => $accountForm->createView(),
  70. 'isValid' => $isValid,
  71. ]);
  72. }
  73.  
  74. private function addViolationMessagesToForm(ConstraintViolationList $violationList, FormInterface &$form)
  75. {
  76. $violationListArray = (array) $violationList;
  77. $violationListViolations = array_pop($violationListArray);
  78.  
  79. foreach ($violationListViolations as $violation) {
  80. $property = str_replace('data.', '', $violation->getPropertyPath());
  81. $form->get($property)->addError(new FormError($violation->getMessage()));
  82. }
  83. }
  84. }
  85.  
  86. // validate manually when the user is logged in
  87. $form->submit([], false);
  88. $violationList = $this->get('validator')->validate($form);
  89. $isValid = (empty($violationList)) ? true : false;
  90. $this->addViolationMessagesToForm($violationList, $form);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement