Although I cannot comment if this is the best way or not, I always passed them to my task constructor as a hard dependency ...
Services
services: my_bundle.form.type.task: class: Company\MyBundle\Form\Type\TaskType arguments: - @doctrine.orm.entity_manager
controller
$form = $this->createForm($this->get('my_bundle.form.type.task'), $task); // or $form = $this->createForm(new TaskType($this->getDoctrine()->getEntityManager()));
Type of form
namespace Company\MyBundle\Form\Type; use Doctrine\ORM\EntityManager; use Symfony\Component\Form\AbstractType;
Once my form types have any dependencies, I use the container to manage them. I personally find this method much clearer of what is going on and what my custom classes require than relying on Symfonyโs complex form configuration to do this for me.
source share