From a98e484020baac07d0f45e3322433c2dcd51bc12 Mon Sep 17 00:00:00 2001 From: Alexander Schwenn Date: Tue, 5 May 2015 03:19:13 +0200 Subject: [PATCH] [Form] Fix ChoiceType with legacy ChoiceList --- .../ChoiceList/Factory/DefaultChoiceListFactory.php | 12 ++++++++++-- .../Factory/DefaultChoiceListFactoryTest.php | 10 ++++++---- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php b/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php index 907829be0e..ce4e52c016 100644 --- a/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php +++ b/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php @@ -20,6 +20,7 @@ use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView; use Symfony\Component\Form\ChoiceList\View\ChoiceListView; use Symfony\Component\Form\ChoiceList\View\ChoiceView; use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface as LegacyChoiceListInterface; +use Symfony\Component\Form\Extension\Core\View\ChoiceView as LegacyChoiceView; /** * Default implementation of {@link ChoiceListFactoryInterface}. @@ -140,9 +141,16 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null) { // Backwards compatibility - if ($list instanceof LegacyChoiceListInterface && null === $preferredChoices + if ($list instanceof LegacyChoiceListInterface && empty($preferredChoices) && null === $label && null === $index && null === $groupBy && null === $attr) { - return new ChoiceListView($list->getRemainingViews(), $list->getPreferredViews()); + $mapToNonLegacyChoiceView = function (LegacyChoiceView $choiceView) { + return new ChoiceView($choiceView->label, $choiceView->value, $choiceView->data); + }; + + return new ChoiceListView( + array_map($mapToNonLegacyChoiceView, $list->getRemainingViews()), + array_map($mapToNonLegacyChoiceView, $list->getPreferredViews()) + ); } $preferredViews = array(); diff --git a/src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php b/src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php index 3519bd8f21..5812a23e79 100644 --- a/src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php +++ b/src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php @@ -18,6 +18,7 @@ use Symfony\Component\Form\ChoiceList\LazyChoiceList; use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView; use Symfony\Component\Form\ChoiceList\View\ChoiceListView; use Symfony\Component\Form\ChoiceList\View\ChoiceView; +use Symfony\Component\Form\Extension\Core\View\ChoiceView as LegacyChoiceView; class DefaultChoiceListFactoryTest extends \PHPUnit_Framework_TestCase { @@ -735,8 +736,9 @@ class DefaultChoiceListFactoryTest extends \PHPUnit_Framework_TestCase */ public function testCreateViewForLegacyChoiceList() { - $preferred = array(new ChoiceView('Preferred', 'x', 'x')); - $other = array(new ChoiceView('Other', 'y', 'y')); + // legacy ChoiceList instances provide legacy ChoiceView objects + $preferred = array(new LegacyChoiceView('x', 'x', 'Preferred')); + $other = array(new LegacyChoiceView('y', 'y', 'Other')); $list = $this->getMock('Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface'); @@ -749,8 +751,8 @@ class DefaultChoiceListFactoryTest extends \PHPUnit_Framework_TestCase $view = $this->factory->createView($list); - $this->assertSame($other, $view->choices); - $this->assertSame($preferred, $view->preferredChoices); + $this->assertEquals(array(new ChoiceView('Other', 'y', 'y')), $view->choices); + $this->assertEquals(array(new ChoiceView('Preferred', 'x', 'x')), $view->preferredChoices); } private function assertScalarListWithGeneratedValues(ChoiceListInterface $list)