Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 040c4d0

Browse files
committed
[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 040c4d0

3 files changed

Lines changed: 39 additions & 4 deletions

File tree

src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/DoctrineChoiceLoaderTest.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ protected function setUp(): void
8888
;
8989

9090
$this->objectLoader = $this->createMock(EntityLoaderInterface::class);
91-
$this->obj1 = (object) ['name' => 'A'];
92-
$this->obj2 = (object) ['name' => 'B'];
93-
$this->obj3 = (object) ['name' => 'C'];
91+
$this->obj1 = (object) ['name' => 'A', 'id' => 1];
92+
$this->obj2 = (object) ['name' => 'B', 'id' => 2];
93+
$this->obj3 = (object) ['name' => 'C', 'id' => 3];
9494

9595
$this->om->expects($this->any())
9696
->method('getRepository')
@@ -190,6 +190,22 @@ public function testLoadValuesForChoicesDoesNotLoadIfEmptyChoices()
190190
$this->assertSame([], $loader->loadValuesForChoices([]));
191191
}
192192

193+
public function testLoadValuesForChoicesCastsTypesToStringWithValueCallback()
194+
{
195+
$loader = new DoctrineChoiceLoader(
196+
$this->om,
197+
$this->class,
198+
$this->idReader
199+
);
200+
201+
$this->repository->expects($this->never())
202+
->method('findAll');
203+
204+
$this->assertSame(['2', '3'], $loader->loadValuesForChoices([$this->obj2, $this->obj3], function ($item) {
205+
return $item->id;
206+
}));
207+
}
208+
193209
/**
194210
* @group legacy
195211
*/

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ 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) {
63+
return (string) $value($item);
64+
}, $choices);
6365
}
6466

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

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,23 @@ 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) {
101+
return $item->id;
102+
};
103+
104+
$this->assertSame(
105+
['2', '3'],
106+
self::$loader->loadValuesForChoices($choices, $value)
107+
);
108+
}
109+
93110
public function testLoadValuesForChoicesLoadsChoiceListOnFirstCall()
94111
{
95112
$this->assertSame(

0 commit comments

Comments
 (0)