Quantcast
Channel: Symfony Blog
Viewing all articles
Browse latest Browse all 3058

New in Symfony 5.3: Form Field Sorting

$
0
0
Yonel Ceruto

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.phpnamespaceApp\Form\Type;useSymfony\Component\Form\AbstractType;useSymfony\Component\Form\Extension\Core\Type\DateType;useSymfony\Component\Form\Extension\Core\Type\SubmitType;useSymfony\Component\Form\Extension\Core\Type\TextType;useSymfony\Component\Form\FormBuilderInterface;classTaskTypeextendsAbstractType{publicfunctionbuildForm(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.

Viewing all articles
Browse latest Browse all 3058

Trending Articles