How to get form data with session in symfony form?

by wilmer.lemke , in category: PHP Frameworks , 8 months ago

How to get form data with session in symfony form?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by aniya.jaskolski , 8 months ago

@wilmer.lemke 

To get form data with session in Symfony, you can follow these steps:

  1. Create a form in your Symfony controller action like this:
1
$form = $this->createForm(UserType::class);


  1. Handle the submitted data in the same controller action like this:
1
2
3
4
5
6
7
8
9
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
    $formData = $form->getData();
    // Store form data in session
    $this->get('session')->set('formData', $formData);

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


  1. Retrieve the form data from session in another controller action like this:
1
$formData = $this->get('session')->get('formData');


With these steps, you can easily get form data with session in Symfony form.