网页 综合 更新时间 2026-11-13 08:16:00

New in Symfony 8.2: Validator Improvements (Symfony Blog)

自动蜘蛛 symfony.com 正文约 4163 字
New in Symfony 8.2: Validator Improvements (Symfony Blog)
November 13, 2026 • Published by Javier Eguiluz
Symfony 8.2 is backed by:
The Callback constraint is the usual way to define custom validation rules, but it requires a lot of code for simple rules. In Symfony 8.2, the Callback constraint adds a message option. When it's set, the callback only has to return a boolean and Symfony creates the violation when it returns false.
Combined with PHP 8.5, which allows static closures in constant expressions, you can now define the whole rule inside the attribute:
The fields option of the Unique constraint checks the uniqueness of a collection based only on some of its fields. However, it only worked for collections of arrays. In Symfony 8.2, it also works for collections of objects:
Public properties are read directly and the rest of the values are read with the PropertyAccess component, so getters and property paths work too.
PHP 8.4 added the BcMath\Number class to work with arbitrary precision numbers, which is common in financial applications. In Symfony 8.2, the Range constraint and the comparison constraints (EqualTo, NotEqualTo, GreaterThan, GreaterThanOrEqual, LessThan and LessThanOrEqual) support these values:
Comparing a BcMath\Number with a float in PHP truncates the float to an integer (new Number('10.2') > 10.5 is true). That's why Symfony converts the limits to BcMath\Number objects before comparing them, so the precision is kept on both sides.
When creating violations in custom constraint validators, the setParameter() method only accepted strings, while setParameters() accepted any value. This was inconsistent, especially when using the ICU message format in your translations, which can format numbers and dates. In Symfony 8.2, setParameter() accepts string, int, float, \Stringable and \DateTimeInterface values:
Group sequences validate the groups of a class step by step. In the sequence, the group of the class itself had to be written as its short class name (e.g. 'Order'), a plain string that is easy to miss when renaming the class. In Symfony 8.2, you can use the ::class constant of the class instead.
In addition, when a class defines a group sequence, the objects referenced with Valid are validated only against the Default group, so their constraints in the other groups of the sequence never run. The new cascadeCurrentGroup option validates those objects against the current group of the sequence too:
This option is also available in the YAML and XML formats, and in the #[Assert\GroupSequenceProvider] attribute for classes that compute their sequence dynamically.
The groups option of the Valid constraint does two things at once: it decides when to validate the nested object and it restricts which of its constraints are checked. This makes it hard to create reusable nested objects, because the parent object decides which constraints run on them. In Symfony 8.2, set the new restrictGroups option to false to use the groups only to trigger the cascade:
The other improvement is that Valid can now be used inside the Sequentially constraint. This is useful for values that can have more than one shape and should only be cascaded after checking some other constraint first:
The nested Valid constraint can't define its own groups; set them in the Sequentially constraint instead.
❤️ 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.
💼 CTO for a Symfony project at OpenDecide View Symfony jobs →
€75,000 – €85,000 / year France Remote + part-time onsite (Toulouse, Paris, France)
This is amazing! Thank you much!
Code consumes server resources. Blackfire tells you how
Peruse our complete Symfony & PHP solutions catalog for your web development needs.
Save your teams and projects before they sink
Thanks Adrian Rudnik (@kreischweide) for being a Symfony contributor
8 commits • 155 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.
访问原文 返回搜索 https://symfony.com/blog/new-in-symfony-8-2-validator-improvements
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。