Skip to content

Commit a98e484

Browse files
committed
[Form] Fix ChoiceType with legacy ChoiceList
1 parent dfba29a commit a98e484

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Form\ChoiceList\View\ChoiceListView;
2121
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
2222
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface as LegacyChoiceListInterface;
23+
use Symfony\Component\Form\Extension\Core\View\ChoiceView as LegacyChoiceView;
2324

2425
/**
2526
* Default implementation of {@link ChoiceListFactoryInterface}.
@@ -140,9 +141,16 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, $value = nul
140141
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null)
141142
{
142143
// Backwards compatibility
143-
if ($list instanceof LegacyChoiceListInterface && null === $preferredChoices
144+
if ($list instanceof LegacyChoiceListInterface && empty($preferredChoices)
144145
&& null === $label && null === $index && null === $groupBy && null === $attr) {
145-
return new ChoiceListView($list->getRemainingViews(), $list->getPreferredViews());
146+
$mapToNonLegacyChoiceView = function (LegacyChoiceView $choiceView) {
147+
return new ChoiceView($choiceView->label, $choiceView->value, $choiceView->data);
148+
};
149+
150+
return new ChoiceListView(
151+
array_map($mapToNonLegacyChoiceView, $list->getRemainingViews()),
152+
array_map($mapToNonLegacyChoiceView, $list->getPreferredViews())
153+
);
146154
}
147155

148156
$preferredViews = array();

src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
1919
use Symfony\Component\Form\ChoiceList\View\ChoiceListView;
2020
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
21+
use Symfony\Component\Form\Extension\Core\View\ChoiceView as LegacyChoiceView;
2122

2223
class DefaultChoiceListFactoryTest extends \PHPUnit_Framework_TestCase
2324
{
@@ -735,8 +736,9 @@ function ($object, $key, $value) {
735736
*/
736737
public function testCreateViewForLegacyChoiceList()
737738
{
738-
$preferred = array(new ChoiceView('Preferred', 'x', 'x'));
739-
$other = array(new ChoiceView('Other', 'y', 'y'));
739+
// legacy ChoiceList instances provide legacy ChoiceView objects
740+
$preferred = array(new LegacyChoiceView('x', 'x', 'Preferred'));
741+
$other = array(new LegacyChoiceView('y', 'y', 'Other'));
740742

741743
$list = $this->getMock('Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface');
742744

@@ -749,8 +751,8 @@ public function testCreateViewForLegacyChoiceList()
749751

750752
$view = $this->factory->createView($list);
751753

752-
$this->assertSame($other, $view->choices);
753-
$this->assertSame($preferred, $view->preferredChoices);
754+
$this->assertEquals(array(new ChoiceView('Other', 'y', 'y')), $view->choices);
755+
$this->assertEquals(array(new ChoiceView('Preferred', 'x', 'x')), $view->preferredChoices);
754756
}
755757

756758
private function assertScalarListWithGeneratedValues(ChoiceListInterface $list)

0 commit comments

Comments
 (0)