8000 [Form] Cast choices value callback result to string · enumag/symfony@be58048 · GitHub
[go: up one dir, main page]

Skip to content

Commit be58048

Browse files
Matth--nicolas-grekas
authored andcommitted
[Form] Cast choices value callback result to string
If using "multiple" and "choice_value" on a CollectionType or EntityType, the result for the callback needs to be a string. By forcing a string cast, default values will show up in the form view.
1 parent bf901b8 commit be58048

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/Symfony/Component/Form/ChoiceList/Loader/AbstractChoiceLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function loadValuesForChoices(array $choices, callable $value = null)
5959

6060
if ($value) {
6161
// if a value callback exists, use it
62-
return array_map($value, $choices);
62+
return array_map(function ($item) use ($value): string { return $value($item); }, $choices);
6363
}
6464

6565
return $this->doLoadValuesForChoices($choices);

src/Symfony/Component/Form/Tests/ChoiceList/Loader/CallbackChoiceLoaderTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ public function testLoadChoicesForValuesLoadsChoiceListOnFirstCall()
9090
);
9191
}
9292

93+
public function testLoadValuesForChoicesCastsCallbackItemsToString()
94+
{
95+
$choices = [
96+
(object) ['id' => 2],
97+
(object) ['id' => 3],
98+
];
99+
100+
$value = function ($item) { return $item->id; };
101+
102+
$this->assertSame(['2', '3'], self::$loader->loadValuesForChoices($choices, $value));
103+
}
104+
93105
public function testLoadValuesForChoicesLoadsChoiceListOnFirstCall()
94106
{
95107
$this->assertSame(

0 commit comments

Comments
 (0)
0