From b946b11d5a199a726cbbdabd7d725c45f00ecb0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20ALFAIATE?= Date: Thu, 27 Jul 2017 11:03:55 +0200 Subject: [PATCH 1/2] [Form] Repeat preferred choices in the main list --- .../Form/ChoiceList/Factory/DefaultChoiceListFactory.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php b/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php index 68da1b2516b8d..7fe1f46247c4a 100644 --- a/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php +++ b/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php @@ -157,9 +157,9 @@ private static function addChoiceView($choice, $value, $label, $keys, &$index, $ if ($isPreferred && false !== $preferredKey = $isPreferred($choice, $key, $value)) { $preferredViews[$nextIndex] = $view; $preferredViewsOrder[$nextIndex] = $preferredKey; - } else { - $otherViews[$nextIndex] = $view; } + + $otherViews[$nextIndex] = $view; } private static function addChoiceViewsFromStructuredValues($values, $label, $choices, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$preferredViewsOrder, &$otherViews) From 475c7a469ab373eaa0f1666f773ba2abd3602e96 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Mon, 22 Jul 2019 16:05:04 +0200 Subject: [PATCH 2/2] adapt tests --- .../Tests/Form/Type/EntityTypeTest.php | 4 +-- src/Symfony/Bridge/Doctrine/composer.json | 4 +-- .../AbstractBootstrap3LayoutTest.php | 11 +++++--- src/Symfony/Bridge/Twig/composer.json | 4 +-- src/Symfony/Component/Form/CHANGELOG.md | 1 + .../Form/Tests/AbstractLayoutTest.php | 11 +++++--- .../Factory/DefaultChoiceListFactoryTest.php | 26 +++++++++++++++++-- .../Extension/Core/Type/ChoiceTypeTest.php | 4 +++ 8 files changed, 49 insertions(+), 16 deletions(-) diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php index 0a9bf739fc224..90906ef6e09ec 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php @@ -848,7 +848,7 @@ public function testPreferredChoices() ]); $this->assertEquals([3 => new ChoiceView($entity3, '3', 'Baz'), 2 => new ChoiceView($entity2, '2', 'Bar')], $field->createView()->vars['preferred_choices']); - $this->assertEquals([1 => new ChoiceView($entity1, '1', 'Foo')], $field->createView()->vars['choices']); + $this->assertEquals([1 => new ChoiceView($entity1, '1', 'Foo'), 2 => new ChoiceView($entity2, '2', 'Bar'), 3 => new ChoiceView($entity3, '3', 'Baz')], $field->createView()->vars['choices']); } public function testOverrideChoicesWithPreferredChoices() @@ -868,7 +868,7 @@ public function testOverrideChoicesWithPreferredChoices() ]); $this->assertEquals([3 => new ChoiceView($entity3, '3', 'Baz')], $field->createView()->vars['preferred_choices']); - $this->assertEquals([2 => new ChoiceView($entity2, '2', 'Bar')], $field->createView()->vars['choices']); + $this->assertEquals([2 => new ChoiceView($entity2, '2', 'Bar'), 3 => new ChoiceView($entity3, '3', 'Baz')], $field->createView()->vars['choices']); } public function testDisallowChoicesThatAreNotIncludedChoicesSingleIdentifier() diff --git a/src/Symfony/Bridge/Doctrine/composer.json b/src/Symfony/Bridge/Doctrine/composer.json index d022f57c32de2..3256c521fd34e 100644 --- a/src/Symfony/Bridge/Doctrine/composer.json +++ b/src/Symfony/Bridge/Doctrine/composer.json @@ -27,7 +27,7 @@ "symfony/stopwatch": "^3.4|^4.0|^5.0", "symfony/config": "^4.2|^5.0", "symfony/dependency-injection": "^3.4|^4.0|^5.0", - "symfony/form": "^4.3|^5.0", + "symfony/form": "^4.4|^5.0", "symfony/http-kernel": "^3.4|^4.0|^5.0", "symfony/messenger": "^4.3|^5.0", "symfony/property-access": "^3.4|^4.0|^5.0", @@ -48,7 +48,7 @@ "conflict": { "phpunit/phpunit": "<4.8.35|<5.4.3,>=5.0", "symfony/dependency-injection": "<3.4", - "symfony/form": "<4.3", + "symfony/form": "<4.4", "symfony/messenger": "<4.3" }, "suggest": { diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap3LayoutTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap3LayoutTest.php index b332ff018d742..f3a0a381cd630 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap3LayoutTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap3LayoutTest.php @@ -524,8 +524,9 @@ public function testSingleChoiceWithPreferred() ./option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"] /following-sibling::option[@disabled="disabled"][not(@selected)][.="-- sep --"] /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"] + /following-sibling::option[@value="&b"][.="[trans]Choice&B[/trans]"] ] - [count(./option)=3] + [count(./option)=4] ' ); } @@ -547,8 +548,9 @@ public function testSingleChoiceWithPreferredAndNoSeparator() [ ./option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"] /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"] + /following-sibling::option[@value="&b"][.="[trans]Choice&B[/trans]"] ] - [count(./option)=2] + [count(./option)=3] ' ); } @@ -571,8 +573,9 @@ public function testSingleChoiceWithPreferredAndBlankSeparator() ./option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"] /following-sibling::option[@disabled="disabled"][not(@selected)][.=""] /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"] + /following-sibling::option[@value="&b"][.="[trans]Choice&B[/trans]"] ] - [count(./option)=3] + [count(./option)=4] ' ); } @@ -589,7 +592,7 @@ public function testChoiceWithOnlyPreferred() $this->assertWidgetMatchesXpath($form->createView(), ['attr' => ['class' => 'my&class']], '/select [@class="my&class form-control"] - [count(./option)=2] + [count(./option)=5] ' ); } diff --git a/src/Symfony/Bridge/Twig/composer.json b/src/Symfony/Bridge/Twig/composer.json index c4e6ab29d4ee1..de4b75e05d12b 100644 --- a/src/Symfony/Bridge/Twig/composer.json +++ b/src/Symfony/Bridge/Twig/composer.json @@ -25,7 +25,7 @@ "symfony/asset": "^3.4|^4.0|^5.0", "symfony/dependency-injection": "^3.4|^4.0|^5.0", "symfony/finder": "^3.4|^4.0|^5.0", - "symfony/form": "^4.3|^5.0", + "symfony/form": "^4.4|^5.0", "symfony/http-foundation": "^4.3|^5.0", "symfony/http-kernel": "^3.4|^4.0|^5.0", "symfony/mime": "^4.3|^5.0", @@ -46,7 +46,7 @@ }, "conflict": { "symfony/console": "<3.4", - "symfony/form": "<4.3", + "symfony/form": "<4.4", "symfony/http-foundation": "<4.3", "symfony/translation": "<4.2", "symfony/workflow": "<4.3" diff --git a/src/Symfony/Component/Form/CHANGELOG.md b/src/Symfony/Component/Form/CHANGELOG.md index 527f84b44a0a5..a082f1fa60392 100644 --- a/src/Symfony/Component/Form/CHANGELOG.md +++ b/src/Symfony/Component/Form/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG 4.4.0 ----- + * preferred choices are repeated in the list of all choices * deprecated using `int` or `float` as data for the `NumberType` when the `input` option is set to `string` 4.3.0 diff --git a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php index b03ac0f9fc4d9..8154893fbf130 100644 --- a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php @@ -741,8 +741,9 @@ public function testSingleChoiceWithPreferred() ./option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"] /following-sibling::option[@disabled="disabled"][not(@selected)][.="-- sep --"] /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"] + /following-sibling::option[@value="&b"][.="[trans]Choice&B[/trans]"] ] - [count(./option)=3] + [count(./option)=4] ' ); } @@ -763,8 +764,9 @@ public function testSingleChoiceWithPreferredAndNoSeparator() [ ./option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"] /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"] + /following-sibling::option[@value="&b"][.="[trans]Choice&B[/trans]"] ] - [count(./option)=2] + [count(./option)=3] ' ); } @@ -786,8 +788,9 @@ public function testSingleChoiceWithPreferredAndBlankSeparator() ./option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"] /following-sibling::option[@disabled="disabled"][not(@selected)][.=""] /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"] + /following-sibling::option[@value="&b"][.="[trans]Choice&B[/trans]"] ] - [count(./option)=3] + [count(./option)=4] ' ); } @@ -803,7 +806,7 @@ public function testChoiceWithOnlyPreferred() $this->assertWidgetMatchesXpath($form->createView(), [], '/select - [count(./option)=2] + [count(./option)=5] ' ); } diff --git a/src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php b/src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php index b065718054112..7073890d6bcf4 100644 --- a/src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php +++ b/src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php @@ -739,6 +739,8 @@ private function assertFlatView($view) $this->assertEquals(new ChoiceListView( [ 0 => new ChoiceView($this->obj1, '0', 'A'), + 1 => new ChoiceView($this->obj2, '1', 'B'), + 2 => new ChoiceView($this->obj3, '2', 'C'), 3 => new ChoiceView($this->obj4, '3', 'D'), ], [ 1 => new ChoiceView($this->obj2, '1', 'B'), @@ -752,6 +754,8 @@ private function assertFlatViewWithCustomIndices($view) $this->assertEquals(new ChoiceListView( [ 'w' => new ChoiceView($this->obj1, '0', 'A'), + 'x' => new ChoiceView($this->obj2, '1', 'B'), + 'y' => new ChoiceView($this->obj3, '2', 'C'), 'z' => new ChoiceView($this->obj4, '3', 'D'), ], [ 'x' => new ChoiceView($this->obj2, '1', 'B'), @@ -765,6 +769,18 @@ private function assertFlatViewWithAttr($view) $this->assertEquals(new ChoiceListView( [ 0 => new ChoiceView($this->obj1, '0', 'A'), + 1 => new ChoiceView( + $this->obj2, + '1', + 'B', + ['attr1' => 'value1'] + ), + 2 => new ChoiceView( + $this->obj3, + '2', + 'C', + ['attr2' => 'value2'] + ), 3 => new ChoiceView($this->obj4, '3', 'D'), ], [ 1 => new ChoiceView( @@ -789,11 +805,17 @@ private function assertGroupedView($view) [ 'Group 1' => new ChoiceGroupView( 'Group 1', - [0 => new ChoiceView($this->obj1, '0', 'A')] + [ + 0 => new ChoiceView($this->obj1, '0', 'A'), + 1 => new ChoiceView($this->obj2, '1', 'B'), + ] ), 'Group 2' => new ChoiceGroupView( 'Group 2', - [3 => new ChoiceView($this->obj4, '3', 'D')] + [ + 2 => new ChoiceView($this->obj3, '2', 'C'), + 3 => new ChoiceView($this->obj4, '3', 'D'), + ] ), ], [ 'Group 1' => new ChoiceGroupView( diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php index 83633eed0dec9..b9ecf57583c79 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php @@ -1731,7 +1731,9 @@ public function testPassPreferredChoicesToView() $this->assertEquals([ 0 => new ChoiceView('a', 'a', 'A'), + 1 => new ChoiceView('b', 'b', 'B'), 2 => new ChoiceView('c', 'c', 'C'), + 3 => new ChoiceView('d', 'd', 'D'), ], $view->vars['choices']); $this->assertEquals([ 1 => new ChoiceView('b', 'b', 'B'), @@ -1750,9 +1752,11 @@ public function testPassHierarchicalChoicesToView() $this->assertEquals([ 'Symfony' => new ChoiceGroupView('Symfony', [ 0 => new ChoiceView('a', 'a', 'Bernhard'), + 1 => new ChoiceView('b', 'b', 'Fabien'), 2 => new ChoiceView('c', 'c', 'Kris'), ]), 'Doctrine' => new ChoiceGroupView('Doctrine', [ + 3 => new ChoiceView('d', 'd', 'Jon'), 4 => new ChoiceView('e', 'e', 'Roman'), ]), ], $view->vars['choices']);