From 3388c5be13755d435ec401bb061f069cce7cbfca Mon Sep 17 00:00:00 2001 From: Gonzalo Vilaseca Date: Thu, 2 Mar 2017 14:06:59 +0000 Subject: [PATCH] Document callable for objects It's shown in the `Advanced Example (with Objects!)`, but not explained in the `choice_label` section. --- .../forms/types/options/choice_label.rst.inc | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/reference/forms/types/options/choice_label.rst.inc b/reference/forms/types/options/choice_label.rst.inc index 0dbf1f1e89d..fe31280404a 100644 --- a/reference/forms/types/options/choice_label.rst.inc +++ b/reference/forms/types/options/choice_label.rst.inc @@ -34,7 +34,7 @@ will give you: .. image:: /_images/reference/form/choice-example2.png :align: center -If your choice values are objects, then ``choice_label`` can also be a +If your choice values are objects, then ``choice_label`` can either be a callable or a :ref:`property path `. Imagine you have some ``Status`` class with a ``getDisplayName()`` method:: @@ -49,6 +49,20 @@ If your choice values are objects, then ``choice_label`` can also be a ), 'choice_label' => 'displayName', )); +The callable version receives the object as the first parameter: + use Symfony\Component\Form\Extension\Core\Type\ChoiceType; + // ... + $builder->add('attending', ChoiceType::class, array( + 'choices' => array( + new Status(Status::YES), + new Status(Status::NO), + new Status(Status::MAYBE), + ), + 'choice_label' => function($status, $key, $index) { + /** @var Status $status */ + return $status->getDisplayName; + }, + )); If set to ``false``, all the tag labels will be discarded for radio or checkbox inputs. You can also return ``false`` from the callable to discard certain labels.