Skip to content

Document callable for objects #7565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion reference/forms/types/options/choice_label.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <reference-form-option-property-path>`. Imagine you have some
``Status`` class with a ``getDisplayName()`` method::

Expand All @@ -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.