[Form] Fix ChoiceType with legacy ChoiceList

This commit is contained in:
Alexander Schwenn 2015-05-05 03:19:13 +02:00
parent dfba29a901
commit a98e484020
2 changed files with 16 additions and 6 deletions

View File

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

View File

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