Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ce8398f

Browse filesBrowse files
committed
Add support for sorting fields (weighted sort)
1 parent bb1e1e5 commit ce8398f
Copy full SHA for ce8398f

File tree

9 files changed

+53
-2
lines changed
Filter options

9 files changed

+53
-2
lines changed

‎src/Symfony/Component/Form/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ CHANGELOG
1515
* Added a `choice_translation_parameters` option to `ChoiceType`
1616
* Add `UuidType` and `UlidType`
1717
* Dependency on `symfony/intl` was removed. Install `symfony/intl` if you are using `LocaleType`, `CountryType`, `CurrencyType`, `LanguageType` or `TimezoneType`.
18+
* Add `weight` option and sorting form fields.
1819

1920
5.2.0
2021
-----

‎src/Symfony/Component/Form/Extension/Core/Type/BaseType.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/BaseType.php
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
107107
'translation_domain' => $translationDomain,
108108
'label_translation_parameters' => $labelTranslationParameters,
109109
'attr_translation_parameters' => $attrTranslationParameters,
110+
'weight' => $options['weight'],
110111
// Using the block name here speeds up performance in collection
111112
// forms, where each entry has the same full block name.
112113
// Including the type is important too, because if rows of a
@@ -135,11 +136,13 @@ public function configureOptions(OptionsResolver $resolver)
135136
'attr' => [],
136137
'translation_domain' => null,
137138
'auto_initialize' => true,
139+
'weight' => 0,
138140
]);
139141

140142
$resolver->setAllowedTypes('block_prefix', ['null', 'string']);
141143
$resolver->setAllowedTypes('attr', 'array');
142144
$resolver->setAllowedTypes('row_attr', 'array');
143145
$resolver->setAllowedTypes('label_html', 'bool');
146+
$resolver->setAllowedTypes('weight', 'int');
144147
}
145148
}

‎src/Symfony/Component/Form/Form.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Form.php
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,11 +1044,29 @@ public function createView(FormView $parent = null)
10441044
$view->children[$name] = $child->createView($view);
10451045
}
10461046

1047+
$this->sort($view->children);
1048+
10471049
$type->finishView($view, $this, $options);
10481050

10491051
return $view;
10501052
}
10511053

1054+
/**
1055+
* Sorts view's children based on their weight value.
1056+
*/
1057+
private function sort(array &$children): void
1058+
{
1059+
$c = [];
1060+
$n = 0;
1061+
foreach ($children as $name => $child) {
1062+
$c[$name] = ['w' => $child->vars['weight'], 'n' => $n++];
1063+
}
1064+
1065+
uksort($children, static function ($a, $b) use ($c): int {
1066+
return $c[$a]['w'] === $c[$b]['w'] ? $c[$a]['n'] <=> $c[$b]['n'] : $c[$a]['w'] <=> $c[$b]['w'];
1067+
});
1068+
}
1069+
10521070
/**
10531071
* Normalizes the underlying data if a model transformer is set.
10541072
*

‎src/Symfony/Component/Form/Tests/CompoundFormTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/CompoundFormTest.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,10 +958,12 @@ public function testCreateViewWithChildren()
958958
->getForm();
959959
$view = new FormView();
960960
$field1View = new FormView();
961+
$field1View->vars['weight'] = 0;
961962
$type1
962963
->method('createView')
963964
->willReturn($field1View);
964965
$field2View = new FormView();
966+
$field2View->vars['weight'] = 0;
965967
$type2
966968
->method('createView')
967969
->willReturn($field2View);

‎src/Symfony/Component/Form/Tests/Extension/Core/Type/FormTypeTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Core/Type/FormTypeTest.php
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,29 @@ public function testFormAttrAsStringWithNoId()
836836
$this->assertSame($view->vars['id'], $view['child1']->vars['attr']['form']);
837837
$this->assertSame($view->vars['id'], $view['child2']->vars['attr']['form']);
838838
}
839+
840+
public function testSortingViewChildrenBasedOnWeightOption()
841+
{
842+
$view = $this->factory->createNamedBuilder('parent', self::TESTED_TYPE)
843+
->add('child1', null, ['weight' => 1])
844+
->add('child2')
845+
->add('child3', null, ['weight' => 1])
846+
->add('child4')
847+
->add('child5', null, ['weight' => -1])
848+
->add('child6')
849+
->getForm()
850+
->createView();
851+
852+
$expected = [
853+
'child5',
854+
'child2',
855+
'child4',
856+
'child6',
857+
'child1',
858+
'child3',
859+
];
860+
$this->assertSame($expected, array_keys($view->children));
861+
}
839862
}
840863

841864
class Money

‎src/Symfony/Component/Form/Tests/Fixtures/Descriptor/resolved_form_type_1.json

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Fixtures/Descriptor/resolved_form_type_1.json
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262
"row_attr",
6363
"setter",
6464
"translation_domain",
65-
"upload_max_size_message"
65+
"upload_max_size_message",
66+
"weight"
6667
]
6768
},
6869
"extension": {

‎src/Symfony/Component/Form/Tests/Fixtures/Descriptor/resolved_form_type_1.txt

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Fixtures/Descriptor/resolved_form_type_1.txt
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Symfony\Component\Form\Extension\Core\Type\ChoiceType (Block prefix: "choice")
4040
setter
4141
translation_domain
4242
upload_max_size_message
43+
weight
4344
------------------------------- -------------------- ------------------------------ -----------------------
4445

4546
Parent types

‎src/Symfony/Component/Form/Tests/Fixtures/Descriptor/resolved_form_type_2.json

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Fixtures/Descriptor/resolved_form_type_2.json
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
"setter",
4242
"translation_domain",
4343
"trim",
44-
"upload_max_size_message"
44+
"upload_max_size_message",
45+
"weight"
4546
],
4647
"overridden": [],
4748
"parent": [],

‎src/Symfony/Component/Form/Tests/Fixtures/Descriptor/resolved_form_type_2.txt

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Fixtures/Descriptor/resolved_form_type_2.txt
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,6 @@ Symfony\Component\Form\Extension\Core\Type\FormType (Block prefix: "form")
4444
translation_domain
4545
trim
4646
upload_max_size_message
47+
weight
4748
------------------------------
4849

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.