feature #32658 [Form] repeat preferred choices in list of all choices (Seb33300, xabbuh)

This PR was merged into the 4.4 branch.

Discussion
----------

[Form] repeat preferred choices in list of all choices

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #10271
| License       | MIT
| Doc PR        |

Commits
-------

475c7a469a adapt tests
b946b11d5a [Form] Repeat preferred choices in the main list
This commit is contained in:
Nicolas Grekas 2019-07-24 16:40:02 +02:00
commit 5af1a90d94
9 changed files with 51 additions and 18 deletions

View File

@ -848,7 +848,7 @@ class EntityTypeTest extends BaseTypeTest
]);
$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 @@ class EntityTypeTest extends BaseTypeTest
]);
$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()

View File

@ -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": {

View File

@ -524,8 +524,9 @@ abstract class AbstractBootstrap3LayoutTest extends AbstractLayoutTest
./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 @@ abstract class AbstractBootstrap3LayoutTest extends AbstractLayoutTest
[
./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 @@ abstract class AbstractBootstrap3LayoutTest extends AbstractLayoutTest
./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 @@ abstract class AbstractBootstrap3LayoutTest extends AbstractLayoutTest
$this->assertWidgetMatchesXpath($form->createView(), ['attr' => ['class' => 'my&class']],
'/select
[@class="my&class form-control"]
[count(./option)=2]
[count(./option)=5]
'
);
}

View File

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

View File

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

View File

@ -157,9 +157,9 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface
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)

View File

@ -741,8 +741,9 @@ abstract class AbstractLayoutTest extends FormIntegrationTestCase
./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 @@ abstract class AbstractLayoutTest extends FormIntegrationTestCase
[
./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 @@ abstract class AbstractLayoutTest extends FormIntegrationTestCase
./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 @@ abstract class AbstractLayoutTest extends FormIntegrationTestCase
$this->assertWidgetMatchesXpath($form->createView(), [],
'/select
[count(./option)=2]
[count(./option)=5]
'
);
}

View File

@ -739,6 +739,8 @@ class DefaultChoiceListFactoryTest extends TestCase
$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 @@ class DefaultChoiceListFactoryTest extends TestCase
$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 @@ class DefaultChoiceListFactoryTest extends TestCase
$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 @@ class DefaultChoiceListFactoryTest extends TestCase
[
'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(

View File

@ -1731,7 +1731,9 @@ class ChoiceTypeTest extends BaseTypeTest
$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 @@ class ChoiceTypeTest extends BaseTypeTest
$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']);