title: Symfony : How to hydrate a form with the data of another without having been persisted in the database
body: <p>I develop with Symfony 4</p>
  
  <p>I currently have a search form which returns to another page in which we find the same form (to be able to modify the search), and I would like to hydrate the second form with the data filled in the first form, knowing that data are not persisted in the database.</p>
  
  <p>My search form :</p>
  
  <pre><code>public function buildForm(FormBuilderInterface $builder, array $options)
  {
      $builder
          -&gt;add('position', TextType::class, [
              'label'    =&gt; 'object.position',
              'required' =&gt; false,
          ])
          -&gt;add ('city', TextType::class, [
              'label'    =&gt; 'object.city',
              'required' =&gt; false,
          ])
          -&gt;add ('postalCode', TextType::class, [
              'label'    =&gt; 'object.postalCode',
              'required' =&gt; false,
          ])
          -&gt;add('sectors', EntityType::class, [
              'placeholder'  =&gt; 'Selectionnez les secteurs d\'activité',
              'class'        =&gt; Sector::class,
              'choice_label' =&gt; function($sectors) {
                  return $sectors-&gt;getTitle();
              },
              'multiple' =&gt; true,
              'label'    =&gt; 'object.sector',
              'required' =&gt; false,
          ])
          -&gt;add('professions', EntityType::class, [
              'placeholder'  =&gt; 'Selectionnez les métiers',
              'class'        =&gt; Profession::class,
              'choice_label' =&gt; function($professions) {
                  return $professions-&gt;getTitle();
              },
              'multiple' =&gt; true,
              'label'    =&gt; 'object.profession',
              'required' =&gt; false,
          ])
          -&gt;add('salary', ChoiceType::class, [
              'placeholder' =&gt; 'Selectionnez une tranche de salaire minimum',
              'choices'     =&gt; [
                  '10K€ à 20K€ par an' =&gt; 1,
                  '21K€ à 30K€ par an' =&gt; 2,
                  '31K€ à 40K€ par an' =&gt; 3,
                  '41K€ à 50K€ par an' =&gt; 4,
                  '51K€ à 60K€ par an' =&gt; 5,
                  '&gt; à 60K€ par an'    =&gt; 6,
              ],
              'label'    =&gt; 'object.salary',
              'required' =&gt; false,
          ])
          -&gt;add('contracts', EntityType::class, [
              'placeholder'  =&gt; 'Selectionnez les types de contrat',
              'class'        =&gt; Contract::class,
              'choice_label' =&gt; function($contracts) {
                  return $contracts-&gt;getTitle();
              },
              'multiple' =&gt; true,
              'label'    =&gt; 'object.contract',
              'required' =&gt; false,
          ])
          -&gt;add('experience', ChoiceType::class, [
              'placeholder' =&gt; 'Selectionnez l\'expérience maximum requise',
              'choices'     =&gt; [
                  'Etudiant / En formation' =&gt; 1,
                  'Débutant'                =&gt; 2,
                  '2 ans et plus'           =&gt; 3,
                  '3 ans'                   =&gt; 4,
                  '4 ans'                   =&gt; 5,
                  '5 ans et plus'           =&gt; 6,
              ],
              'label'    =&gt; 'object.experience',
              'required' =&gt; false,
          ])
          -&gt;add('training', ChoiceType::class, [
              'placeholder' =&gt; 'Selectionnez le niveau de diplôme maximum requis',
              'choices'     =&gt; [
                  'Sans diplôme'       =&gt; 1,
                  'CAP - BEP'          =&gt; 2,
                  'Bac Pro'            =&gt; 3,
                  'DUT - BTS - Bac +2' =&gt; 4,
                  'Bac +3'             =&gt; 5,
                  'Bac +4 - Bac +5'    =&gt; 6,
              ],
              'label'    =&gt; 'object.training',
              'required' =&gt; false,
          ])
          -&gt;add('handicapped', CheckboxType::class, [
              'required' =&gt; false,
              'label'    =&gt; 'object.handicapped',
          ])
          -&gt;add('homeOffice', CheckboxType::class, [
              'required' =&gt; false,
              'label'    =&gt; 'object.handicapped',
          ])
          -&gt;add('checked', CheckboxType::class, [
              'required' =&gt; false,
              'label'    =&gt; 'object.checked',
          ])
          -&gt;add('titleSearch', TextType::class, [
              'required' =&gt; false,
              'label'    =&gt; 'object.title'
          ])
      ;
  }
  </code></pre>
  
  <p>First controller</p>
  
  <pre><code>public function indexJob(Request $request, Candidate $candidate): Response
  {
      $jobSearch = new JobSearch();
      $formSearch = $this-&gt;createForm(JobSearchFormType::class);
      $formSearch-&gt;handleRequest($request);
  
      if ($formSearch-&gt;isSubmitted() &amp;&amp; $formSearch-&gt;isValid()) {
          $data = $formSearch-&gt;getData();
          $checked = $data-&gt;checked;
  
          $_SESSION['data'] = $formSearch-&gt;getData();
  
          return $this-&gt;redirectToRoute('candidate_job_search', [
              'candidate_slug' =&gt; $candidate-&gt;getSlug()
          ]);
      }
      return $this-&gt;render('job/index.html.twig', [
          'candidate'  =&gt; $candidate,
          'form'       =&gt; $formSearch-&gt;createView(),
      ]);
  }
  </code></pre>
  
  <p>My second controller</p>
  
  <pre><code>public function searchJob(Request $request, Candidate $candidate, JobRepository $jobRepository, PaginatorInterface $paginator): Response
  {
      $sessionData = $_SESSION['data'];
      $jobSearch = new JobSearch();
      $formSearch = $this-&gt;createForm(JobSearchFormType::class, $sessionData);
      $formSearch-&gt;handleRequest($request);
  
      if ($formSearch-&gt;isSubmitted() &amp;&amp; $formSearch-&gt;isValid()) {
          $data = $formSearch-&gt;getData();
          $checked = $data-&gt;checked;
  
          $_SESSION['data'] = $formSearch-&gt;getData();
  
      }
      return $this-&gt;render('job/search.html.twig', [
          'candidate'  =&gt; $candidate,
          'formSearch' =&gt; $formSearch-&gt;createView(),
          'jobs'       =&gt; $paginator-&gt;paginate(
              $jobRepository-&gt;findBySearchField($sessionData),
              $request-&gt;query-&gt;getInt('page', 1),
              10),
      ]);
  }
  </code></pre>
  
  <p>My Repository</p>
  
  <pre><code>public function findBySearchField($data): array
  {
      $queryBuilder = $this-&gt;createQueryBuilder('j');
  
      $queryBuilder-&gt;innerJoin('j.company', 'c', 'WITH', 'c.isActive = true')
          -&gt;addSelect('c')
          -&gt;andWhere('j.isActive = true')
          -&gt;orderBy('j.postedAt', 'DESC');
  
      $searchTerms = $this-&gt;extractSearchTerms($data-&gt;position);
  
      if (!empty($data-&gt;position)) {
          foreach ($searchTerms as $key =&gt; $term) {
              $queryBuilder
                  -&gt;andWhere('j.position LIKE :p_'.$key)
                  -&gt;setParameter('p_'.$key, '%'.$term.'%')
              ;
          }
      }
      if (!empty($data-&gt;city)) {
          $queryBuilder-&gt;andWhere('j.city = :cit')
              -&gt;setParameter(':cit', $data-&gt;city);
      }
      if (!empty($data-&gt;postalCode)) {
          $queryBuilder-&gt;andWhere('j.postalCode = :pos')
              -&gt;setParameter(':pos', $data-&gt;postalCode);
      }
      if ($data-&gt;professions-&gt;count() &gt; 0) {
          $queryBuilder-&gt;innerJoin('j.profession', 'pro')
              -&gt;addSelect('pro')
              -&gt;andWhere('pro.id IN (:professionIds)')
              -&gt;setParameter('professionIds', $data-&gt;professions);
      }
      if ($data-&gt;sectors-&gt;count() &gt; 0) {
          $queryBuilder-&gt;innerJoin('j.sector', 'sec')
              -&gt;addSelect('sec')
              -&gt;andWhere('sec.id IN (:sectorIds)')
              -&gt;setParameter('sectorIds', $data-&gt;sectors);
      }
      if ($data-&gt;contracts-&gt;count() &gt; 0) {
          $queryBuilder-&gt;innerJoin('j.contract', 'con')
              -&gt;addSelect('con')
              -&gt;andWhere('con.id IN (:contractIds)')
              -&gt;setParameter('contractIds', $data-&gt;contracts);
      }
      if (!empty($data-&gt;salary)) {
          $queryBuilder-&gt;andWhere('j.salary &gt;= :sal')
              -&gt;setParameter('sal', $data-&gt;salary);
      }
      if (!empty($data-&gt;experience)) {
          $queryBuilder-&gt;andWhere('j.experience = :exp')
              -&gt;setParameter('exp', $data-&gt;experience);
      }
      if (!empty($data-&gt;training)) {
          $queryBuilder-&gt;andWhere('j.training = :tra')
              -&gt;setParameter('tra', $data-&gt;training);
      }
      if (!empty($data-&gt;homeOffice)) {
          $queryBuilder-&gt;andWhere('j.homeOffice = :hom')
              -&gt;setParameter('hom', $data-&gt;homeOffice);
      }
      if (!empty($data-&gt;handicapped)) {
          $queryBuilder-&gt;andWhere('j.handicapped = :han')
              -&gt;setParameter('han', $data-&gt;handicapped);
      }
      return $queryBuilder
          -&gt;getQuery()
          -&gt;getResult();
  }
  </code></pre>
  
  <p>My problem is that when I launch the search I get the error:</p>
  
  <blockquote>
    <p>Entity of type "App\Entity\Sector" passed to the choice field must be managed. Maybe you forget to persist it in the entity manager?</p>
  </blockquote>
  
  <p>I tried to find a way to solve this problem but I did not succeed</p>