0

I have a register form.

When the form is submitted and valid there is a bunch of validation happening like checking if the password and password_confirm match. If not I redirect to the same page with the error message.

But I don't want my user to loose all the data he already filled. How can I fill the form with those data. Should I use a FormEventListener?

public function register(Request $request, RegisterHandler $registerHandler, Register $register)
{
    if($this->getUser()){
        return $this->redirectToRoute('homepage');
    }

    $form = $this->createForm(RegisterType::class, $register);

    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        try{
            $registerHandler->handle($form->getData());
        }catch (UserAlreadyExistException $e){
            $this->addFlash('error', $e->getMessage());
            return $this->redirectToRoute('register');
        }catch (PasswordMatchingException $e){
            $this->addFlash('error', $e->getMessage());
            return $this->redirectToRoute('register');
        }

        return $this->redirectToRoute('homepage');
    }

    return $this->render('app/pages/login/register.html.twig', [
        'form' => $form->createView(),
    ]);
}

My Form Type

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('firstName', TextType::class, [
            'required' => true,
            'label' => 'Your Name',
            'attr' => [
                'class' => 'form-control',
            ]
        ])
        ->add('lastName', TextType::class, [
            'required' => true,
            'label' => 'Your Last Name',
            'attr' => [
                'class' => 'form-control',
            ]
        ])
        ->add('email', EmailType::class, [
            'required' => true,
            'label' => 'Your Email',
            'attr' => [
                'class' => 'form-control',
            ]
        ])
        ->add('password', PasswordType::class, [
            'required' => true,
            'label' => 'Your password',
            'attr' => [
                'id' => 'password1',
                'class' => 'form-control',
            ]
        ])
        ->add('passwordConfirm', PasswordType::class, [
            'required' => true,
            'label' => 'Confirm password',
            'attr' => [
                'id' => 'password2',
                'class' => 'form-control',
            ]
        ])
    ;

    $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
        $data = $event->getData();
        $form = $event->getForm();

        //@TODO
    });
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'data_class' => Register::class,
    ]);
}
1
  • 1
    Since you are already in the /register there is no need to redirect. Just remove the redirects to 'register' route and it should work. You cannot redirect and keep post data at same time. Form events are not needed. Commented Oct 23, 2018 at 7:58

1 Answer 1

2

Where do PasswordMatchingException and UserAlreadyExistException come from?

Usually you would use a constraint or write your own validator, then $form->isValid() would be false and the form would be rendered again with the same data and display the errors. For UserAlreadyExistException you would use @UniqueEntity("email") and for passwords you should use RepeatedType.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.