New in Symfony 5.3: Form Field Sorting

Contributed by Yonel Ceruto in #40690.

Symfony forms render their fields in the same order that you define them. This might cause issues when adding/removing fields dynamically in complex forms (e.g. a field added via form events can end up being rendered after the form submit button). That’s why in Symfony 5.3 you can sort form fields to control the order in which they are rendered. To do so, use the new priority option, which is a positive or negative integer with a default value of 0. Fields with higher priorities are rendered first (and fields with same priority are rendered in their original order): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22// src/Form/Type/TaskType.php namespace App\Form\Type;

use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\DateType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface;

class TaskType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options): void { $builder ->add('task', TextType::class, ['priority' => 150]) ->add('dueDate', DateType::class) ->add('save', SubmitType::class, [ 'priority' => $options['show_first'] ? 1000 : -1000, ]) ; } }

                Sponsor the Symfony project.

http://feedproxy.google.com/~r/symfony/blog/~3/hDvwaPt-kPo/new-in-symfony-5-3-form-field-sorting

Created 4y | May 21, 2021, 7:21:56 AM


Login to add comment

Other posts in this group

New Core Team Members, 2025 Edition

A few weeks ago, I had the pleasure of announcing the formation of the Symfony UX Core Team, a dedicated group working to enhance the frontend development experience within the Symfony ecosystem. Toda

Feb 24, 2025, 4:20:03 PM | Symfony
SymfonyLive Paris 2025 : Du lego de composants pour un bundle Gotenberg !

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

Al

Feb 24, 2025, 1:50:07 PM | Symfony
A Week of Symfony #947 (17-23 February 2025)

This week, development activity focused on new security features. The upcoming Symfony 7.3 version added support for security voters to explain their vote, improved the IsGranted attribute to allow us

Feb 23, 2025, 10:10:09 AM | Symfony
SymfonyLive Berlin 2025: Agentic Applications with Symfony

SymfonyLive Berlin 2025, conference held in English, will take place from April 1 to 4! The schedule is being revealed gradually. More details are available here.

We’re thrilled to announce

Feb 21, 2025, 9:20:12 AM | Symfony
SymfonyLive Paris 2025 : Postgres pour vos besoins NoSQL

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

Al

Feb 20, 2025, 10:10:13 AM | Symfony
SymfonyLive Berlin 2025: Asynchronous PHP

SymfonyLive Berlin 2025, conference held in English, will take place from April 1 to 4! The schedule is being revealed gradually. More details are available here.

We’re thrilled to announce

Feb 19, 2025, 8:40:05 AM | Symfony
SymfonyLive Paris 2025 :  Le Composant Symfony Mapper

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

Al

Feb 18, 2025, 2:10:24 PM | Symfony