From d43ebfd8d0141a35f99d3c059e687d3d5881669d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Hlav=C3=A1=C4=8D?= Date: Wed, 14 Oct 2015 11:06:24 +0200 Subject: [PATCH 1/2] Add note about using choice_label as a callback I was wondering how to call a method with an argument for some time and then I've came with an idea of using ArrayAccess that can be used with PropertyAccessor. Then I found I can actually use callback in `choice_label`, but this was not documented. Is this correct usage? --- reference/forms/types/entity.rst | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/reference/forms/types/entity.rst b/reference/forms/types/entity.rst index 58faabc5ea2..315c8ef9ec8 100644 --- a/reference/forms/types/entity.rst +++ b/reference/forms/types/entity.rst @@ -130,6 +130,17 @@ cast into a string and so must have a ``__toString()`` method. 'choice_label' => 'translations[en].name', )); +.. note:: + The ``choice_label`` option can also be a callback. The callback has + 2 arguments, the entity and index in the list. You can use it like this:: + + $builder->add('gender', 'entity', array( + 'class' => 'MyBundle:Gender', + 'choice_label' => function(MyBundle\Gender $gender) { + return $gender->getLocalizedGender($this->locale); + }, + )); + class ~~~~~ From c5a3ba625da8247baeb8b82f647bf52f3861f75d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Hlav=C3=A1=C4=8D?= Date: Wed, 14 Oct 2015 12:18:49 +0200 Subject: [PATCH 2/2] Add note about using choice_label as a callback / 2. --- reference/forms/types/entity.rst | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/reference/forms/types/entity.rst b/reference/forms/types/entity.rst index 315c8ef9ec8..b5d8ff440d1 100644 --- a/reference/forms/types/entity.rst +++ b/reference/forms/types/entity.rst @@ -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); + }, + )); + .. note:: The ``choice_label`` option is the property path used to display the option. @@ -130,17 +140,6 @@ cast into a string and so must have a ``__toString()`` method. 'choice_label' => 'translations[en].name', )); -.. note:: - The ``choice_label`` option can also be a callback. The callback has - 2 arguments, the entity and index in the list. You can use it like this:: - - $builder->add('gender', 'entity', array( - 'class' => 'MyBundle:Gender', - 'choice_label' => function(MyBundle\Gender $gender) { - return $gender->getLocalizedGender($this->locale); - }, - )); - class ~~~~~