Skip to content

Add note about using choice_label as a callback #5788

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 2 commits 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
12 changes: 11 additions & 1 deletion reference/forms/types/entity.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,22 @@ choice_label
The ``choice_label`` option was introduced in Symfony 2.7. Prior to Symfony
2.7, it was called ``property`` (which has the same functionality).

**type**: ``string``
**type**: ``string|callable``

This is the property that should be used for displaying the entities
as text in the HTML element. If left blank, the entity object will be
cast into a string and so must have a ``__toString()`` method.

The ``choice_label`` option can also be a callback that accepts 2 arguments: entity and index.
You can use it like this::

$builder->add('gender', 'entity', array(
'class' => 'MyBundle:Gender',
'choice_label' => function(MyBundle\Entity\Gender $gender, $index) {
return $index . '. ' . $gender->getLocalizedGender($this->locale);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indention correct here?

},
));

.. note::

The ``choice_label`` option is the property path used to display the option.
Expand Down