Skip to content

Commit e2307a9

Browse files
committed
[#3098] Tweaks to entity "choices" options
Biggest thing was to make it clear that choices is a valid thing to set when using the entity type, but it's not the most common way of providing options.
1 parent f061758 commit e2307a9

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

reference/forms/types/entity.rst

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ objects from the database.
1818
| | - `query_builder`_ |
1919
| | - `em`_ |
2020
+-------------+------------------------------------------------------------------+
21-
| Overridden | - `choices` |
22-
| Options | - `choice_list` |
21+
| Overridden | - `choices`_ |
22+
| Options | - `choice_list`_ |
2323
+-------------+------------------------------------------------------------------+
2424
| Inherited | - `required`_ |
2525
| options | - `label`_ |
@@ -55,17 +55,6 @@ as either a ``select`` tag, a set or radio buttons or a series of checkboxes
5555
If the entity object does not have a ``__toString()`` method the ``property`` option
5656
is needed.
5757

58-
Using Choices
59-
~~~~~~~~~~~~~
60-
61-
If you want to limit the entities which are available as choices you can provide
62-
them by calling the getter function of an entity:
63-
64-
$builder->add('users', 'entity', array(
65-
'class' => 'AcmeHelloBundle:User',
66-
'choices' => $group->getUsers(),
67-
));
68-
6958
Using a Custom Query for the Entities
7059
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7160

@@ -84,6 +73,22 @@ option. The easiest way to use the option is as follows::
8473
},
8574
));
8675

76+
.. _reference-forms-entity-choices:
77+
78+
Using Choices
79+
~~~~~~~~~~~~~
80+
81+
If you already have the exact collection of entities that you want included
82+
in the choice element, you can simply pass them via the ``choices`` key.
83+
For example, if you have a ``$group`` variable (passed into your form perhaps
84+
as a form option) and ``getUsers`` returns a collection of ``User`` entities,
85+
then you can supply the ``choices`` option directly::
86+
87+
$builder->add('users', 'entity', array(
88+
'class' => 'AcmeHelloBundle:User',
89+
'choices' => $group->getUsers(),
90+
));
91+
8792
.. include:: /reference/forms/types/options/select_how_rendered.rst.inc
8893

8994
Field Options
@@ -143,17 +148,21 @@ Overridden Options
143148
choices
144149
~~~~~~~
145150

146-
**type**: array || ``\Traversable`` **default**: all entities
151+
**type**: array || ``\Traversable`` **default**: ``null``
147152

148-
If the choices are provided only these will be available. The choices need to
149-
be a traversable collection such as the
150-
``Doctrine\Common\Collections\ArrayCollection`` containing only entities of the
151-
specified class.
153+
Instead of allowing the `class`_ and `query_builder`_ options to fetch the
154+
entities to include for you, you can pass the ``choices`` option directly.
155+
See :ref:`reference-forms-entity-choices`.
152156

153157
choice_list
154158
~~~~~~~~~~~
155159

156-
**default**: ``null``
160+
**default**: :class:`Symfony\\Bridge\\Doctrine\\Form\\ChoiceList\\EntityChoiceList`
161+
162+
The purpose of the ``entity`` type is to create and configure this ``EntityChoiceList``
163+
for you, by using all of the above options. If you need to override this
164+
option, you may just consider using the :doc:`/reference/forms/types/choice`
165+
directly.
157166

158167
Inherited options
159168
-----------------

0 commit comments

Comments
 (0)