diff --git a/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php b/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php index 1433da2e8742..9e54fa0857f9 100644 --- a/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php +++ b/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php @@ -64,16 +64,18 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null, // Backwards compatibility if ($list instanceof LegacyChoiceListAdapter && empty($preferredChoices) && null === $label && null === $index && null === $groupBy && null === $attr) { - $mapToNonLegacyChoiceView = function (LegacyChoiceView $choiceView) { - return new ChoiceView($choiceView->data, $choiceView->value, $choiceView->label); + $mapToNonLegacyChoiceView = function (LegacyChoiceView &$choiceView) { + $choiceView = new ChoiceView($choiceView->data, $choiceView->value, $choiceView->label); }; $adaptedList = $list->getAdaptedList(); - return new ChoiceListView( - array_map($mapToNonLegacyChoiceView, $adaptedList->getRemainingViews()), - array_map($mapToNonLegacyChoiceView, $adaptedList->getPreferredViews()) - ); + $remainingViews = $adaptedList->getRemainingViews(); + $preferredViews = $adaptedList->getPreferredViews(); + array_walk_recursive($remainingViews, $mapToNonLegacyChoiceView); + array_walk_recursive($preferredViews, $mapToNonLegacyChoiceView); + + return new ChoiceListView($remainingViews, $preferredViews); } $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 e8508ab06fe0..1738aaff0ff3 100644 --- a/src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php +++ b/src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php @@ -737,7 +737,7 @@ function ($object, $key, $value) { /** * @group legacy */ - public function testCreateViewForLegacyChoiceList() + public function testCreateViewForFlatLegacyChoiceList() { // legacy ChoiceList instances provide legacy ChoiceView objects $preferred = array(new LegacyChoiceView('x', 'x', 'Preferred')); @@ -758,6 +758,36 @@ public function testCreateViewForLegacyChoiceList() $this->assertEquals(array(new ChoiceView('x', 'x', 'Preferred')), $view->preferredChoices); } + /** + * @group legacy + */ + public function testCreateViewForNestedLegacyChoiceList() + { + // legacy ChoiceList instances provide legacy ChoiceView objects + $preferred = array('Section 1' => array(new LegacyChoiceView('x', 'x', 'Preferred'))); + $other = array( + 'Section 2' => array(new LegacyChoiceView('y', 'y', 'Other')), + new LegacyChoiceView('z', 'z', 'Other one'), + ); + + $list = $this->getMock('Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface'); + + $list->expects($this->once()) + ->method('getPreferredViews') + ->will($this->returnValue($preferred)); + $list->expects($this->once()) + ->method('getRemainingViews') + ->will($this->returnValue($other)); + + $view = $this->factory->createView(new LegacyChoiceListAdapter($list)); + + $this->assertEquals(array( + 'Section 2' => array(new ChoiceView('y', 'y', 'Other')), + new ChoiceView('z', 'z', 'Other one'), + ), $view->choices); + $this->assertEquals(array('Section 1' => array(new ChoiceView('x', 'x', 'Preferred'))), $view->preferredChoices); + } + private function assertScalarListWithChoiceValues(ChoiceListInterface $list) { $this->assertSame(array('a', 'b', 'c', 'd'), $list->getValues());