title: Symfony 5 Form class Entity Manager
body: <p>I have a form in FormType class and I pass Entity manager throught constructor.</p>
  <p>Here is example of code: Controller</p>
  <pre><code>$form = $this-&gt;createForm(new FormType($em));
  </code></pre>
  <p>here is my controller code :</p>
  <pre><code>&lt;?php
  
  declare(strict_types=1);
  
  namespace App\Controller;
  
  use App\Form\FormulaireType;
  use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Component\HttpFoundation\Response;
  
  class FormulaireController extends AbstractController
  {
      public function nouvelleAction(Request $request): Response
      {
          $session = $request-&gt;getSession();
  
          if (4 != $session-&gt;get('niveau')) {
              throw $this-&gt;createAccessDeniedException();
          }
  
          $em   = $this-&gt;getDoctrine()-&gt;getManager();
          $form = $this-&gt;createForm(FormulaireType::class);
          $form-&gt;handleRequest($request);
  
          if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {
              // Doctrine\ORM\EntityManager instance
              $sm = $this-&gt;getDoctrine()-&gt;getConnection()-&gt;getSchemaManager();
              $em-&gt;getConnection()-&gt;exec('CREATE TABLE custom_'.$form-&gt;get('liste')-&gt;getData().'(lead_id int(9) NOT NULL, PRIMARY KEY (lead_id)) ENGINE = MYISAM');
  
              $this-&gt;get('session')-&gt;getFlashBag()-&gt;add('notice', 'La formulaire de la liste avec identifiant '.$form-&gt;get('liste')-&gt;getData().' a été bien crée');
          }
  
          return $this-&gt;render('formulaire/nouvelle_formulaire.html.twig', ['form' =&gt; $form-&gt;createView()]);
      }
  }
  </code></pre>
  <p>here is my FormType class code :</p>
  <pre><code>&lt;?php
  
  declare(strict_types=1);
  
  namespace App\Form;
  
  use App\Entity\VicidialCampaigns;
  use App\Entity\VicidialLists;
  use Doctrine\ORM\EntityManagerInterface;
  use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\Form\FormEvent;
  use Symfony\Component\Form\FormEvents;
  use Symfony\Component\OptionsResolver\OptionsResolver;
  
  class FormulaireType extends AbstractType
  {
      private $em;
  
      public function __construct(EntityManagerInterface $em)
      {
          $this-&gt;em = $em;
      }
  
      public function buildForm(FormBuilderInterface $builder, array $options): void
      {
          $builder
              -&gt;add('campagne', EntityType::class, ['label' =&gt; 'les campagnes', 'class' =&gt; VicidialCampaigns::class,
                  'choice_label'                            =&gt; 'campaignName', 'attr' =&gt; ['class' =&gt; 'form-control'], ])
              -&gt;add('liste', ChoiceType::class, ['label' =&gt; 'les listes', 'attr' =&gt; ['class' =&gt; 'form-control']]);
  
          $fc = function (FormInterface $form, $campagne): void {
              $listes    = $this-&gt;em-&gt;getRepository(VicidialLists::class)-&gt;findBycampaignId($campagne);
              $leslistes = [];
              if ($listes) {
                  foreach ($listes as $liste) {
                      $leslistes[$liste-&gt;getListId()] = $liste-&gt;getListName();
                  }
              }
  
              $form-&gt;add('liste', ChoiceType::class, ['label' =&gt; 'les listes', 'attr' =&gt; ['class' =&gt; 'form-control'],
                  'choices'                                   =&gt; $leslistes, ]);
          };
          $builder-&gt;get('campagne')-&gt;addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use ($fc): void {
              $fc($event-&gt;getForm()-&gt;getParent(), $event-&gt;getForm()-&gt;getData());
          });
      }
  
      public function configureOptions(OptionsResolver $resolver): void
      {
          $resolver-&gt;setDefaults([
              // Configure your form options here
          ]);
      }
  }
  
  </code></pre>
  <p>Symfony 5 has change in method createForm to:</p>
  <pre><code>$this-&gt;createForm(SubjectType::class);
  </code></pre>
  <p>And with this declaration, I don't know how to pass entity manager to form class.</p>
  <p>Any help ?</p>