8000 [2.7][Form] Fixed ChoiceType with legacy ChoiceList by xelaris · Pull Request #14551 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[2.7][Form] Fixed ChoiceType with legacy ChoiceList #14551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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}.
Expand Down Expand Up @@ -140,9 +141,16 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, $value = nul
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);
8000 Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a more efficient solution would be to simply define the "attr" property dynamically:

$choiceView->attr = array();

After all, this method is called pretty often.

};

return new ChoiceListView(
array_map($mapToNonLegacyChoiceView, $list->getRemainingViews()),
array_map($mapToNonLegacyChoiceView, $list->getPreferredViews())
);
}

$preferredViews = array();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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\View\ChoiceView as LegacyChoiceView;

class DefaultChoiceListFactoryTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -735,8 +736,9 @@ function ($object, $key, $value) {
*/
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');

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

$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)
Expand Down
0