网页
综合
更新时间 2026-11-12 08:06:00
New in Symfony 8.2: Form Improvements (Symfony Blog)
自动蜘蛛
symfony.com
正文约 5712 字
November 12, 2026 • Published by Javier Eguiluz
Symfony 8.2 is backed by:
Symfony 8.2 brings many improvements to the Form component, from new form types and options to more flexible multi-step forms and form events.
Search and filter forms often ask for a range of values: a minimum and maximum price, a start and end date, etc. Until now, you had to add two separate fields and validate their order yourself. Symfony 8.2 adds a BoundsType that renders a lower and an upper bound of the same inner type, the same way RepeatedType renders the same type twice:
The two bounds are regular child forms called from and to, so they map to an array{from: mixed, to: mixed} property or to an object with those properties. If your model uses other names, set property_path in from_options and to_options (e.g. minPrice and maxPrice).
The compare option orders scalars, dates and enums (in their declaration order) natively. For other values, pass a callable that compares the two bounds. The error is reported on the lower bound, which is the one users usually change to fix the range.
Form flows (multi-step forms) only supported a single level of steps. In Symfony 8.2, a step can contain other steps, so you can define a hierarchy of steps. When a step only exists to group other steps, use the new createStepGroup() method:
Group steps don't render any form, so the flow moves directly to their child steps. You can also call setSkip() on a step to skip it and all its child steps based on the submitted data. Nesting only changes the structure and the navigation of the flow: all steps still map their data to the same underlying object.
The form is validated once, when the root form dispatches its POST_SUBMIT event. Child forms dispatch that event before the root form, so a listener on a child form could never know whether the form was valid. Symfony 8.2 adds a new FormEvents::POST_VALIDATE event (form.post_validate), dispatched on each form of the tree after the whole form has been validated:
This event is meant for reusable form types like the one above. For application logic, keep checking isValid() in your controller after calling handleRequest().
Symfony itself now uses this event to hash the password of PasswordType fields that define the hash_property_path option. As a result, the FormTypePasswordHasherExtension class and the registerPassword() and hashPasswords() methods of PasswordHasherListener are deprecated and will be removed in Symfony 9.0.
When a form field that is neither compound nor multiple receives an array, the form rejects it before dispatching the PRE_SUBMIT event. This prevents you from transforming that data in a listener, which is a common need when handling JSON payloads. Symfony 8.2 adds an allow_array_submission option to run your listeners first:
The priority of the listener must be above 0 so it runs before the listeners of the form type. If the data is still an array after the listeners run, the form fails with the same error as before. The option is false by default because, when you enable it, all the PRE_SUBMIT listeners of that field must be ready to receive arrays.
The form_attr option adds the HTML form attribute to a field, so you can render it outside of the <form> element. However, this attribute referenced the id of the <div> that wraps the fields instead of the id of the <form> element, so the option never worked. In Symfony 8.2, the <form> element renders an id when some field uses form_attr:
Forms that don't use form_attr render the same HTML as before. To choose a different id, set the id attribute in the attr option of the root form. If your custom form theme overrides the form_start block, update it to render the new form_id variable.
In addition, you can now override or remove the name attribute of the <form> element with the attr option, like any other attribute:
Form fields can display a help message with the help option, but in a ChoiceType field the help applied to the whole field. Symfony 8.2 adds a choice_help option to describe each choice. It works like choice_label, so it accepts an array, a callable or a property path:
Each radio button or checkbox now renders its help message, linked to the input with an aria-describedby attribute for accessibility. The help is translated with choice_translation_domain, like the labels. Collapsed <select> fields don't render it, because <option> elements can't contain other elements.
In addition, the new sort_choices option sorts the choices alphabetically by their translated label, using the rules of the current locale. This is useful for EnumType fields, whose cases are listed in their declaration order:
Sorting uses the Collator class of the intl PHP extension, so accents, case and numbers are handled correctly (e.g. Level 2 goes before Level 10). Choice groups and preferred choices are sorted too.
❤️ Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
💼 Symfony Developer at Bas Software View Symfony jobs →
€5,500 – €6,500 / month Netherlands Remote + part-time onsite (Eindhoven, Netherlands)
Online Symfony certification, take it now!
Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).
No stress: we've got you covered with our 141 automated quality checks of your code
Thanks Matthieu Danet (@matthieu-tmk) for being a Symfony contributor
1 commit • 2 lines changed
View all contributors that help us make Symfony
Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.
Symfony™ is a trademark of Symfony SAS. All rights reserved.
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。