8000 bug #14551 [2.7][Form] Fixed ChoiceType with legacy ChoiceList (xelaris) · symfony/symfony@e9ecd0e · GitHub
[go: up one dir, main page]

Skip to content

Commit e9ecd0e

Browse files
committed
bug #14551 [2.7][Form] Fixed ChoiceType with legacy ChoiceList (xelaris)
This PR was merged into the 2.7 branch. Discussion ---------- [2.7][Form] Fixed ChoiceType with legacy ChoiceList | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #14382 | License | MIT | Doc PR | The "Backwards compatibility" condition doesn't grap (e.g. when passing a `SimpleChoiceList` as `choice_list` on `ChoiceType`), as the default value for the `ChoiceType` option `preferred_choices` is `array()` instead of `null`. So I changed the condition from `null === $preferredChoices` to `empty($preferredChoices)`. Then there was an issue with accessing `attr` in `form_div_layout.html.twig`, since the deprecated `Symfony\Component\Form\Extension\Core\View\ChoiceView` doesn't provide an `attr` attribute. Since the docblocks of `Symfony\Component\Form\ChoiceList\View\ChoiceListView` state `$choices` and `$preferredChoices` to be instances of `Symfony\Component\Form\ChoiceList\View\ChoiceView` instead of `Symfony\Component\Form\Extension\Core\View\ChoiceView` I fixed the template issue by mapping the deprecated `ChoiceView` objects to the new one with an empty `attr`. @webmozart Could you have a look at it, please? Without this PR the following example would render numeric values as labels: ```php $formBuilder->add('choices', 'choice', array( 'choice_list' => new SimpleChoiceList(array( 'creditcard' => 'Credit card payment', 'cash' => 'Cash payment' )) )); ``` Commits ------- a98e484 [Form] Fix ChoiceType with legacy ChoiceList
2 parents efd68e6 + a98e484 commit e9ecd0e

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