PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, March 5, 2022

[FIXED] How to get form data with session in Symfony Form

 March 05, 2022     php, symfony     No comments   

Issue

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,


Solution

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.



Answered By - vinceAmstoutz
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing