Old approach

$form = $this->createForm(new OnBoardingAgreementType($em), $doc, ["editMode" => true]); 

New approach How to pass $ em to the form designer?

 $form = $this->createForm(OnBoardingAgreementType::class, $doc, ["editMode" => true]); 
  • Maybe you should register a form as a service? The documentation for the forms says: "This is only needed if your form type requires some dependencies to be injected" Example: symfony.com/doc/current/cookbook/form/… - S. Pronin
  • And pass the entity manager there already? I thought about it, but it turns out that all forms need to be registered as services - Serge Esmanovich
  • I think you will not be able to transfer the Entity Manager to the form separately, because it requires a connection to the database and is called through the doctrine, so send the arguments: ["@doctrine"] doctrine to the service arguments: ["@doctrine"] , and in the form class already call the entity manager something like this: $this->var_for_doctrine_from->getManager(); - S. Pronin
  • Ok most likely I will do it - Serge Esmanovich
  • @SergeEsmanovich if you do not want to create a form as a service, you can pass the EntityManager object as an option to the form. Those. in configureOptions () you create a new option with the class EntityManager, and pass it when you call createForm - Hast

1 answer 1

If your forms have dependencies, you will have to create a service out of them. The symfony approach is as follows. Just before, you did not do it the way the symfony developers saw it and that's why you are having problems now.