Skip to content

Commit d9e186c

Browse files
bug #49009 [Form] Cast choices value callback result to string (Matth--)
This PR was merged into the 5.4 branch. Discussion ---------- [Form] Cast choices value callback result to string | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #42394 | License | MIT | Doc PR | X When using "multiple" and "choice_value" callback 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. Commits ------- be580488c7 [Form] Cast choices value callback result to string
2 parents e21648e + b814ca0 commit d9e186c

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

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);

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)