@@ -21,6 +21,7 @@ photos).
21
21
| | - `entry_type `_ |
22
22
| | - `prototype `_ |
23
23
| | - `prototype_name `_ |
24
+ | | - `prototype_options `_ |
24
25
+-------------+-----------------------------------------------------------------------------+
25
26
| Inherited | - `by_reference `_ |
26
27
| options | - `empty_data `_ |
@@ -280,7 +281,11 @@ the empty values will be kept.
280
281
entry_options
281
282
~~~~~~~~~~~~~
282
283
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.
284
289
285
290
This is the array that's passed to the form type specified in the `entry_type `_
286
291
option. For example, if you used the :doc: `ChoiceType </reference/forms/types/choice >`
@@ -304,6 +309,44 @@ type::
304
309
),
305
310
));
306
311
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\C omponent\F orm\E xtension\C ore\T ype\H iddenType ` collection would be able
348
+ to hold specific data html attributes for each entry.
349
+
307
350
entry_type
308
351
~~~~~~~~~~
309
352
@@ -363,6 +406,25 @@ If you have several collections in your form, or worse, nested collections
363
406
you may want to change the placeholder so that unrelated placeholders are
364
407
not replaced with the same value.
365
408
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
+
366
428
Inherited Options
367
429
-----------------
368
430
0 commit comments