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

Létrehozva 4y | 2021. máj. 21. 7:21:56


Jelentkezéshez jelentkezzen be

EGYÉB POSTS Ebben a csoportban

SymfonyOnline June 2025: Demystify the Magic of the Container🪄

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days June 12-13: Online conference days in English. All talks will be available for replay as soon

2025. máj. 13. 15:20:17 | Symfony
New in Symfony 7.3: ObjectMapper Component

Contributed by Antoine Bluchet in

2025. máj. 13. 8:20:10 | Symfony
New in Symfony 7.3: Explaining Security Voter Decisions

Contributed by Nicolas Grekas in

2025. máj. 12. 9:10:10 | Symfony
A Week of Symfony #958 (May 5–11, 2025)

This week, we published the second beta of Symfony 7.3, ahead of its final release later in May 2025. Meanwhile, we continued sharing posts about the new features of Symfony 7.3 and the upcoming Symfo

2025. máj. 11. 9:50:17 | Symfony
Symfony 7.3.0-BETA2 released

Symfony 7.3.0-BETA2 has just been released. This is a pre-release version of Symfony 7.3. If you want to test it in your own applications before its final release, run the following commands:

2025. máj. 10. 13:10:15 | Symfony
New in Symfony 7.3: Mailer Security Improvements

The Symfony Mailer component provides many security-related features like signing and encrypting email messages. In Symfony 7.3 we're pushing those features even further to give you greater control an

2025. máj. 9. 9:20:10 | Symfony
SymfonyOnline June 2025: Detect Hidden Defects: Check Your PHP Tests

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

2025. máj. 8. 14:40:19 | Symfony