|
| 1 | +.. _form-as-services: |
| 2 | + |
| 3 | +How to Access Services or Config from Inside a Form |
| 4 | +=================================================== |
| 5 | + |
| 6 | +Sometimes, you may need to access a :doc:`services </service_container>` or other |
| 7 | +configuration from inside of your form class. To do this, you have 2 options: |
| 8 | + |
| 9 | +1) Pass Options to your Form |
| 10 | +---------------------------- |
| 11 | + |
| 12 | +The simplest way to pass services or configuration to your form is via form *options*. |
| 13 | +Suppose you need to access the ``doctrine.orm.entity_manager`` service so that you |
| 14 | +can make a query. First, allow (in fact, require) a new ``entity_manager`` option |
| 15 | +to be passed to your form:: |
| 16 | + |
| 17 | + // src/AppBundle/Form/TaskType.php |
| 18 | + // ... |
| 19 | + |
| 20 | + class TaskType extends AbstractType |
| 21 | + { |
| 22 | + // ... |
| 23 | + |
| 24 | + public function configureOptions(OptionsResolver $resolver) |
| 25 | + { |
| 26 | + // ... |
| 27 | + |
| 28 | + $resolver->setRequired('entity_manager'); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | +Now that you've done this, you *must* pass an ``entity_manager`` option when you |
| 33 | +create your form:: |
| 34 | + |
| 35 | + // src/AppBundle/Controller/DefaultController.php |
| 36 | + // ... |
| 37 | + |
| 38 | + public function newAction() |
| 39 | + { |
| 40 | + $task = ...; |
| 41 | + $form = $this->createForm(new TaskType(), $task, array( |
| 42 | + 'entity_manager' => $this->get('doctrine.orm.entity_manager') |
| 43 | + )); |
| 44 | + |
| 45 | + // ... |
| 46 | + } |
| 47 | + |
| 48 | +Finally, the ``entity_manager`` option is accessible in the ``$options`` argument |
| 49 | +if your ``buildForm`` method:: |
| 50 | + |
| 51 | + // src/AppBundle/Form/TaskType.php |
| 52 | + // ... |
| 53 | + |
| 54 | + class TaskType extends AbstractType |
| 55 | + { |
| 56 | + public function buildForm(FormBuilderInterface $builder, array $options) |
| 57 | + { |
| 58 | + /** @var \Doctrine\ORM\EntityManager $em */ |
| 59 | + $em = $options['entity_manager']; |
| 60 | + // ... |
| 61 | + } |
| 62 | + |
| 63 | + // ... |
| 64 | + } |
| 65 | + |
| 66 | +Use this method to pass *anything* to your form. |
| 67 | + |
| 68 | +2) Define your Form as a Service |
| 69 | +-------------------------------- |
| 70 | + |
| 71 | +Alternatively, you can define your form class as a service. This is a good idea if |
| 72 | +you want to re-use the form in several places - registering it as a service makes |
| 73 | +this easier. |
| 74 | + |
| 75 | +Suppose you need to access the ``doctrine.orm.entity_manager`` service so that you |
| 76 | +can make a query. First, add this as an argument to your form class:: |
| 77 | + |
| 78 | + // src/AppBundle/Form/TaskType.php |
| 79 | + |
| 80 | + use Doctrine\ORM\EntityManager; |
| 81 | + // ... |
| 82 | + |
| 83 | + class TaskType extends AbstractType |
| 84 | + { |
| 85 | + private $em; |
| 86 | + |
| 87 | + public function __construct(EntityManager $em) |
| 88 | + { |
| 89 | + $this->em = $em; |
| 90 | + } |
| 91 | + |
| 92 | + // ... |
| 93 | + } |
| 94 | + |
| 95 | +Next, register this as a service and tag it with ``form.type``: |
| 96 | + |
| 97 | +.. configuration-block:: |
| 98 | + |
| 99 | + .. code-block:: yaml |
| 100 | +
|
| 101 | + # src/AppBundle/Resources/config/services.yml |
| 102 | + services: |
| 103 | + app.form.type.task: |
| 104 | + class: AppBundle\Form\TaskType |
| 105 | + tags: |
| 106 | + - { name: form.type, alias: app_task } |
| 107 | +
|
| 108 | + .. code-block:: xml |
| 109 | +
|
| 110 | + <!-- src/AppBundle/Resources/config/services.xml --> |
| 111 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 112 | + <container xmlns="http://symfony.com/schema/dic/services" |
| 113 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 114 | + xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> |
| 115 | +
|
| 116 | + <services> |
| 117 | + <service id="app.form.type.task" class="AppBundle\Form\TaskType"> |
| 118 | + <tag name="form.type" alias="app_task" /> |
| 119 | + </service> |
| 120 | + </services> |
| 121 | + </container> |
| 122 | +
|
| 123 | + .. code-block:: php |
| 124 | +
|
| 125 | + // src/AppBundle/Resources/config/services.php |
| 126 | + $container |
| 127 | + ->register( |
| 128 | + 'app.form.type.task', |
| 129 | + 'AppBundle\Form\TaskType' |
| 130 | + ) |
| 131 | + ->addTag('form.type', array( |
| 132 | + 'alias' => 'app_task', |
| 133 | + )) |
| 134 | + ; |
| 135 | +
|
| 136 | +That's it! Use the ``alias`` key from the tag to reference your form:: |
| 137 | + |
| 138 | + // src/AppBundle/Controller/DefaultController.php |
| 139 | + // ... |
| 140 | + |
| 141 | + public function newAction() |
| 142 | + { |
| 143 | + $task = ...; |
| 144 | + $form = $this->createForm('app_task', $task); |
| 145 | + |
| 146 | + // ... |
| 147 | + } |
| 148 | + |
| 149 | +Or, use the from within another form:: |
| 150 | + |
| 151 | + // src/AppBundle/Form/Type/ListType.php |
| 152 | + // ... |
| 153 | + |
| 154 | + class ListType extends AbstractType |
| 155 | + { |
| 156 | + public function buildForm(FormBuilderInterface $builder, array $options) |
| 157 | + { |
| 158 | + // ... |
| 159 | + |
| 160 | + $builder->add('someTask', 'app_task'); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | +Read :ref:`form-cookbook-form-field-service` for more information. |
0 commit comments