Skip to content

[Form] Rename priority to position and allow relative sorting of form elements #40858

@keichinger

Description

@keichinger

Description

With #40690 the new priority sorting has been introduced to the form component. In my opinion this feature is very important for most web apps, to reduce unnecessary complexity of your forms - and allow for greater abstraction.

However, as previously discussed (see #40690 (comment)), I'd love to be able to sort my form elements relative to other elements.
We've already created a bundle that allows this functionality as we were not a big fan of sole integer/priority-based sorting: https://github.com/Becklyn/OrderedFormBundle

The bundle adds the position form option, which accepts the following values:

Value Description
"position" => "first" Places the element as the first element in the form.
"position" => "last" Places the element as the last element in the form.
"position" => 42 A simple sort order (the lower the number, the more at the top it is). Works with any integer.
"position" => ["before" => "otherfield"] Places the field before another one.
"position" => ["after" => "otherfield"] Places the field after another one.

This is basically what I'd love to see being added into the Symfony Form component itself: Rename priority to position and add the relative sorting.

Example

class SomeForm extends AbstractType
{
    /**
     * @inheritDoc
     */
    public function buildForm (FormBuilderInterface $builder, array $options) : void
    {
        $builder
            ->add("sortedInTheMiddle", null, [
				// existing functionality of the previous `priority` option
                "position" => 42,
            ])
            ->add("sortedFirst", null, [
				// new functionality: this field will be the first field
                "position" => "first",
            ])
            ->add("sortedLast", null, [
				// new functionality: this field will be the last field
                "position" => "last",
            ])
            ->add("sortedAfterFirst", null, [
				// new functionality: this field will be placed after `sortedFirst`
                "position" => ["after" => "sortedFirst"],
            ])
            ->add("sortedBeforeLast", null, [
				// new functionality: this field will be placed before `sortedLast`
                "position" => ["before" => "sortedLast"],
            ]);
    }
}

The final order would be:

  • sortedFirst
  • sortedAfterFirst
  • noExplicitSorting
  • sortedInTheMiddle
  • sortedBeforeLast
  • sortedLast

WDYT?

/cc @Nyholm

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions