Skip to content
Advertisement

How to get form data with session in Symfony Form

I’ve been looking for a solution for hours. I’am working with Symfony 3.2.

I’am using Symfony Forms in order to display a data-table (with different filter chosen in the form) with Ajax => No submit button.

I can access to different result of data-table to view more information.

What i want is that when i leave the detail page by clicking on a button and come back on the research page, that i could keep in history all filters that i have chosen.

i wanted use session but actually, it seems has 0 impact. Below is some codes.

Controller:

    public function indexAction(Request $request)
        {
            $form = $this->createSearchForm();
    
            $request->getSession()->set('form_data', $form->getData());

            $form->handleRequest($request);

            $form->setData($request->getSession()->get('form_data'));
   
            $this->datatable($form->getData());
    
            return $this->render('backend/jobOffer/index.html.twig', [
                'form' => $form->createView()
            ]);
        }

FormType

 public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('publicationState', ChoiceType::class, [
                'label'                     => 'backend.job_offer.publication_state',
                'required'                  => false,
                'choices'                   => JobOffer::getPublicationStateChoices(),
                'choice_translation_domain' => 'choices',
            ])
            ->add('job', Select2EntityType::class, [
                'label'         => 'backend.job',
                'required'      => false,
                'class'         => 'AppBundle:Job',
                'text_property' => 'label',
                'remote_route'  => 'job_autocomplete',
            ])
            ->add('ids', HiddenType::class, [
                'required'                  => false,
            ])
   }

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'csrf_protection' => false,
        'method'          => 'GET',
    ]);
}

I have a problem, there is a EntityType (or Select2EntityType) field named “job” in my form builder and i can’t get the content of this field in my session. This field shows a autocomplete data-list after typing 2 letters, and we can choose one of job.

and also, when i refresh the page, i lose all filters, but i am supposed to store them in session ?

Thanks in advance for your help,

Advertisement

Answer

Yes you can use $form->getData() but to do that you need to do this according to the doc here with using if ($form->isSubmitted() && $form->isValid()) { among others.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement