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

[Form] Add support for sorting fields #40690

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
Apr 14, 2021
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
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ CHANGELOG
* Added a `choice_translation_parameters` option to `ChoiceType`
* Add `UuidType` and `UlidType`
* Dependency on `symfony/intl` was removed. Install `symfony/intl` if you are using `LocaleType`, `CountryType`, `CurrencyType`, `LanguageType` or `TimezoneType`.
* Add `priority` option to `BaseType` and sorting view fields

5.2.0
-----
Expand Down
5 changes: 5 additions & 0 deletions 5 src/Symfony/Component/Form/Extension/Core/Type/BaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
'translation_domain' => $translationDomain,
'label_translation_parameters' => $labelTranslationParameters,
'attr_translation_parameters' => $attrTranslationParameters,
'priority' => $options['priority'],
// Using the block name here speeds up performance in collection
// forms, where each entry has the same full block name.
// Including the type is important too, because if rows of a
Expand Down Expand Up @@ -135,11 +136,15 @@ public function configureOptions(OptionsResolver $resolver)
'attr' => [],
'translation_domain' => null,
'auto_initialize' => true,
'priority' => 0,
]);

$resolver->setAllowedTypes('block_prefix', ['null', 'string']);
$resolver->setAllowedTypes('attr', 'array');
$resolver->setAllowedTypes('row_attr', 'array');
$resolver->setAllowedTypes('label_html', 'bool');
$resolver->setAllowedTypes('priority', 'int');

$resolver->setInfo('priority', 'The form rendering priority (higher priorities will be rendered first)');
}
}
27 changes: 27 additions & 0 deletions 27 src/Symfony/Component/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -1044,11 +1044,38 @@ public function createView(FormView $parent = null)
$view->children[$name] = $child->createView($view);
}

$this->sort($view->children);
Copy link
Member

Choose a reason for hiding this comment

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

should this be done before or after finishView ?

Copy link
Member Author

Choose a reason for hiding this comment

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

From my point of view, this should be done before finishView().

  1. This is the final step of the view creation, thus I don't expect any view change after that.
  2. In case you want to change the current order to anything else (custom conditions) you could do it in finishView().

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed, if you have a type extension doing this currently (sorting fields on finishView by a custom criteria), we will break that chance if we sort after finishView.

Copy link
Member

Choose a reason for hiding this comment

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

makes sense


$type->finishView($view, $this, $options);

return $view;
}

/**
* Sorts view fields based on their priority value.
*/
private function sort(array &$children): void
{
$c = [];
$i = 0;
$needsSorting = false;
foreach ($children as $name => $child) {
$c[$name] = ['p' => $child->vars['priority'] ?? 0, 'i' => $i++];

if (0 !== $c[$name]['p']) {
$needsSorting = true;
}
}

if (!$needsSorting) {
return;
}

uksort($children, static function ($a, $b) use ($c): int {
yceruto marked this conversation as resolved.
Show resolved Hide resolved
return [$c[$b]['p'], $c[$a]['i']] <=> [$c[$a]['p'], $c[$b]['i']];
});
}

/**
* Normalizes the underlying data if a model transformer is set.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,29 @@ public function testFormAttrAsStringWithNoId()
$this->assertSame($view->vars['id'], $view['child1']->vars['attr']['form']);
$this->assertSame($view->vars['id'], $view['child2']->vars['attr']['form']);
}

public function testSortingViewChildrenBasedOnPriorityOption()
{
$view = $this->factory->createNamedBuilder('parent', self::TESTED_TYPE)
->add('child1', null, ['priority' => -1])
->add('child2')
->add('child3', null, ['priority' => -1])
->add('child4')
->add('child5', null, ['priority' => 1])
->add('child6')
->getForm()
->createView();

$expected = [
'child5',
'child2',
'child4',
'child6',
'child1',
'child3',
];
$this->assertSame($expected, array_keys($view->children));
}
}

class Money
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"mapped",
"method",
"post_max_size_message",
"priority",
"property_path",
"required",
"row_attr",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Symfony\Component\Form\Extension\Core\Type\ChoiceType (Block prefix: "choice")
mapped
method
post_max_size_message
priority
property_path
required
row_attr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"mapped",
"method",
"post_max_size_message",
"priority",
"property_path",
"required",
"row_attr",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Symfony\Component\Form\Extension\Core\Type\FormType (Block prefix: "form")
mapped
method
post_max_size_message
priority
property_path
required
row_attr
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.