diff --git a/src/Symfony/Component/Form/ChoiceList/Loader/AbstractChoiceLoader.php b/src/Symfony/Component/Form/ChoiceList/Loader/AbstractChoiceLoader.php index a98cc239f55e5..1eae069b29dcc 100644 --- a/src/Symfony/Component/Form/ChoiceList/Loader/AbstractChoiceLoader.php +++ b/src/Symfony/Component/Form/ChoiceList/Loader/AbstractChoiceLoader.php @@ -59,7 +59,7 @@ public function loadValuesForChoices(array $choices, callable $value = null) if ($value) { // if a value callback exists, use it - return array_map($value, $choices); + return array_map(function ($item) use ($value): string { return $value($item); }, $choices); } return $this->doLoadValuesForChoices($choices); diff --git a/src/Symfony/Component/Form/Tests/ChoiceList/Loader/CallbackChoiceLoaderTest.php b/src/Symfony/Component/Form/Tests/ChoiceList/Loader/CallbackChoiceLoaderTest.php index 69eb787a23dfa..40f098886b95e 100644 --- a/src/Symfony/Component/Form/Tests/ChoiceList/Loader/CallbackChoiceLoaderTest.php +++ b/src/Symfony/Component/Form/Tests/ChoiceList/Loader/CallbackChoiceLoaderTest.php @@ -90,6 +90,18 @@ public function testLoadChoicesForValuesLoadsChoiceListOnFirstCall() ); } + public function testLoadValuesForChoicesCastsCallbackItemsToString() + { + $choices = [ + (object) ['id' => 2], + (object) ['id' => 3], + ]; + + $value = function ($item) { return $item->id; }; + + $this->assertSame(['2', '3'], self::$loader->loadValuesForChoices($choices, $value)); + } + public function testLoadValuesForChoicesLoadsChoiceListOnFirstCall() { $this->assertSame(