Skip to content

Commit fd2e620

Browse files
committed
[Form] update 'entry_options' and 'prototype_options' in CollectionType
1 parent 1f4113f commit fd2e620

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

reference/forms/types/collection.rst

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ photos).
2121
| | - `entry_type`_ |
2222
| | - `prototype`_ |
2323
| | - `prototype_name`_ |
24+
| | - `prototype_options`_ |
2425
+-------------+-----------------------------------------------------------------------------+
2526
| Inherited | - `by_reference`_ |
2627
| options | - `empty_data`_ |
@@ -280,7 +281,11 @@ the empty values will be kept.
280281
entry_options
281282
~~~~~~~~~~~~~
282283

283-
**type**: ``array`` **default**: ``array()``
284+
**type**: ``array`` or ``callable`` **default**: ``array()``
285+
286+
.. versionadded:: 3.1
287+
288+
Since version 3.1, ``entry_options`` can be set dynamically by a callable.
284289

285290
This is the array that's passed to the form type specified in the `entry_type`_
286291
option. For example, if you used the :doc:`ChoiceType </reference/forms/types/choice>`
@@ -304,6 +309,44 @@ type::
304309
),
305310
));
306311

312+
The above example output each field of the collection with the same choices.
313+
When defined as a callable, each entry of the collection is passed to it as
314+
only argument and it must return a corresponding array of options::
315+
316+
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
317+
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
318+
// ...
319+
320+
$builder->add('favorite_places', CollectionType::class, array(
321+
'entry_type' => ChoiceType::class,
322+
'entry_options' => function ($choice) {
323+
$options = array();
324+
325+
if ($choice instanceof \AppBundle\Entity\City) {
326+
$options = array(
327+
'choices' => $choice->getFamousPlaces(),
328+
'choice_value' => 'id',
329+
'choice_label' => 'localName',
330+
'choice_attr' => function ($place) {
331+
$url = '/places/'.(null === $place ? '' : $place->getSlug();
332+
333+
return array(
334+
'class' => 'place_option',
335+
'attr' => array('data-url' => $url),
336+
);
337+
},
338+
);
339+
}
340+
341+
return $options;
342+
},
343+
'data' => $visitedCities,
344+
));
345+
346+
Many form type have useful options that can be handy to set dynamically for
347+
each entries. A :class:`Symfony\Component\Form\Extension\Core\Type\HiddenType` collection would be able
348+
to hold specific data html attributes for each entry.
349+
307350
entry_type
308351
~~~~~~~~~~
309352

@@ -363,6 +406,25 @@ If you have several collections in your form, or worse, nested collections
363406
you may want to change the placeholder so that unrelated placeholders are
364407
not replaced with the same value.
365408

409+
prototype_options
410+
~~~~~~~~~~~~~~~~~
411+
412+
**type**: ``array`` **default**: ``entry_options`` value
413+
414+
.. versionadded:: 3.1
415+
416+
The ``prototype_options`` option was added in Symfony 3.1.
417+
418+
You can pass any options that will override the corresponding default set in
419+
``entry_options`` option. Can be useful to pass custom css classes and data
420+
html attributes with the ``attr`` option to your prototype.
421+
422+
.. note::
423+
424+
Since version 3.1, you should use a ``data`` key in ``prototype_options``
425+
to define a default data for each new prototype instead of the deprecated
426+
``prototype_data`` option.
427+
366428
Inherited Options
367429
-----------------
368430

0 commit comments

Comments
 (0)