diff --git a/src/Symfony/Component/Form/Extension/Core/ChoiceList/ObjectChoiceList.php b/src/Symfony/Component/Form/Extension/Core/ChoiceList/ObjectChoiceList.php index abf94b514c60b..cf479efca7e20 100644 --- a/src/Symfony/Component/Form/Extension/Core/ChoiceList/ObjectChoiceList.php +++ b/src/Symfony/Component/Form/Extension/Core/ChoiceList/ObjectChoiceList.php @@ -88,7 +88,15 @@ class ObjectChoiceList extends ChoiceList public function __construct($choices, $labelPath = null, array $preferredChoices = array(), $groupPath = null, $valuePath = null, PropertyAccessorInterface $propertyAccessor = null) { $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor(); - $this->labelPath = null !== $labelPath ? new PropertyPath($labelPath) : null; + if ($labelPath !== null) { + if ($labelPath instanceof \Closure) { + $this->labelPath = $labelPath; + } else { + $this->labelPath = new PropertyPath($labelPath); + } + } else { + $this->labelPath = null; + } $this->groupPath = null !== $groupPath ? new PropertyPath($groupPath) : null; $this->valuePath = null !== $valuePath ? new PropertyPath($valuePath) : null; @@ -248,6 +256,9 @@ private function extractLabels($choices, array &$labels) if (is_array($choice)) { $labels[$i] = array(); $this->extractLabels($choice, $labels[$i]); + } elseif ($this->labelPath instanceof \Closure) { + $clousure = $this->labelPath; + $labels[$i] = $clousure($choice); } elseif ($this->labelPath) { $labels[$i] = $this->propertyAccessor->getValue($choice, $this->labelPath); } elseif (method_exists($choice, '__toString')) {