Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace AppController;
- use SymfonyBundleFrameworkBundleControllerController;
- use AppFormUserType;
- use AppEntityUser;
- // and more use ...
- class RegisterController extends Controller
- {
- /**
- * @Route("/{_locale}/register/", name="register", requirements={"_locale"="%app.locales%"})
- *
- * @param Request $request
- * @param UserPasswordEncoderInterface $encoder
- *
- * @return SymfonyComponentHttpFoundationResponse
- */
- public function index(Request $request, UserPasswordEncoderInterface $encoder)
- {
- $isValid = true;
- $doAccountLoginValue = 2;
- $isUserLoggedIn = $this->isGranted('IS_AUTHENTICATED_FULLY');
- $accountForm = $this->getAccountForm();
- $accountForm->handleRequest($request);
- // check if the user wants to login
- if ($accountForm->isSubmitted() &&
- $accountForm->isValid() &&
- intval($accountForm->get('login')->getData()) === $doAccountLoginValue &&
- !$isUserLoggedIn
- ) {
- if ($this->userLogin($request, $encoder)) {
- $isUserLoggedIn = true;
- } else {
- $this->addFlash(
- 'error',
- 'Login incorrect. Invalid email or password.'
- );
- }
- }
- // create the user form. When the user is logged in pass the user object to the UserType
- if ($this->getUser()) {
- $user = $this->getUser();
- } else {
- $user = new User();
- }
- $form = $this->createForm(UserType::class, $user, [
- 'action' => $this->generateUrl('register'),
- ]);
- if ($isUserLoggedIn) {
- // validate manually when the user is logged in
- $form->submit([], false);
- $violationList = $this->get('validator')->validate($form);
- $isValid = (empty($violationList)) ? true : false;
- $this->addViolationMessagesToForm($violationList, $form);
- } else {
- // not logged in, so handle the request with its POST data
- $form->handleRequest($request);
- if ($form->isSubmitted()) {
- $isValid = $form->isValid();
- }
- }
- return $this->render('register/index.html.twig', [
- 'form' => $form->createView(),
- 'accountForm' => $accountForm->createView(),
- 'isValid' => $isValid,
- ]);
- }
- private function addViolationMessagesToForm(ConstraintViolationList $violationList, FormInterface &$form)
- {
- $violationListArray = (array) $violationList;
- $violationListViolations = array_pop($violationListArray);
- foreach ($violationListViolations as $violation) {
- $property = str_replace('data.', '', $violation->getPropertyPath());
- $form->get($property)->addError(new FormError($violation->getMessage()));
- }
- }
- }
- // validate manually when the user is logged in
- $form->submit([], false);
- $violationList = $this->get('validator')->validate($form);
- $isValid = (empty($violationList)) ? true : false;
- $this->addViolationMessagesToForm($violationList, $form);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement