Skip to the stories
AI & ML

Symfony 8.2 Validator Adds Precise Numeric Checks and More Flexible Cascades

  • Javier Eguiluz
  • 575 reads

Symfony 8.2 makes custom rules shorter, supports precise decimal validation and gives teams finer control over nested objects and validation groups.

Symfony 8.2 Validator Adds Precise Numeric Checks and More Flexible Cascades

Symfony 8.2 focuses its Validator changes on reducing friction in rules that already exist: custom callbacks need less boilerplate, decimal comparisons can preserve precision, and nested objects can be checked with more control over validation groups. For teams maintaining complex domain models, the cascade and group sequence updates may have the biggest effect on how validation behavior is organized.

Javier Eguiluz

Custom callback rules can return a boolean

The Callback constraint has traditionally supported custom validation, but even a simple condition required code to build and add a violation. Symfony 8.2 adds a message option that lets a callback return a boolean instead. A false result creates a violation using that message; a true result passes the rule.

With PHP 8.5, static closures can appear in constant expressions, so developers can put a complete rule directly in an attribute. For example, a delivery date can be checked against today without a separate validator method:

FORT Pierre-Louis
#[Assert\Callback(
    static function (\DateTimeImmutable $value): bool {
        return $value > new \DateTimeImmutable('today');
    },
    message: 'The delivery date must be in the future.',
)]
public \DateTimeImmutable $deliveryDate;

The callback still receives the arguments it could receive before, including the validation context. That makes it possible to compare a property with another value on the same object. A total, for instance, can be checked against the order’s subtotal by reading the object through the context. The shorter form suits rules that are local and easy to understand; more involved validation can still live in a dedicated method or validator.

Uniqueness checks now cover object collections

The Unique constraint already had a fields option for checking selected values in a collection of arrays. Symfony 8.2 extends that behavior to collections of objects. A cart can now require each item to have a distinct SKU by applying #[Assert\Unique(fields: ['sku'])] to its item collection.

MD. Saiful Islam Feroz

Symfony reads public properties directly. For other values, it uses the PropertyAccess component, which means getters and property paths can also supply the fields being compared. This removes a practical mismatch between array-shaped data and object-shaped domain models. Teams can express the uniqueness rule where the collection is defined instead of first reshaping objects just to validate them.

Range checks preserve decimal precision

Symfony 8.2 adds support for PHP 8.4’s BcMath\Number type in the Range constraint and the comparison constraints: EqualTo, NotEqualTo, GreaterThan, GreaterThanOrEqual, LessThan and LessThanOrEqual. That matters for applications handling values such as prices or exchange rates, where rounding or truncation can change whether a boundary check passes.

Pascal CESCON

There’s a specific PHP behavior behind the change. Comparing a BcMath\Number with a float can truncate the float to an integer; in the example documented for this change, a value of 10.2 compares as greater than the float 10.5. Symfony avoids that mismatch by converting comparison limits into BcMath\Number objects too, keeping both sides in the same precise numeric representation.

A payment amount can therefore use Range with minimum and maximum bounds, while an exchange rate can use GreaterThanOrEqual with a decimal string. Existing numeric constraints remain the interface developers work with; Symfony handles conversion before comparison. Teams adopting the type should still review their value representation and boundaries, particularly where older code relies on floats.

naxvog

Violation parameters accept dates and numbers

Custom constraint validators can now pass more than strings to setParameter(). Symfony 8.2 accepts integers, floats, \Stringable values and \DateTimeInterface instances as well. Previously, setParameters() accepted arbitrary values, while the single-parameter method only accepted strings.

This change is useful with ICU message formatting, which can format dates and numbers in translated messages. A validator can pass a booking deadline as a date object, then use a message such as Bookings must be made before {deadline, date, long}. The formatting layer receives the original typed value instead of requiring the validator to turn it into a string first. That keeps date presentation in the translation and formatting path, where locale-specific output can be handled.

Nicolas Grekas

Group sequences and cascades gain finer control

Symfony’s group sequences run validation groups in order. The class’s own group previously had to be written as a short string such as 'Order', which could become stale after a class rename. Symfony 8.2 also allows the class constant, as in Order::class, making that reference easier to maintain.

A second group sequence change affects nested objects marked with Valid. By default, those objects were checked only against the Default group, so constraints assigned to later groups in the parent’s sequence did not run on the nested object. The new cascadeCurrentGroup option lets the nested object be checked against the sequence’s current group as well. A shipping address can then run its Shipping constraints during the parent’s Shipping step. The option is also available in YAML and XML configuration and with the GroupSequenceProvider attribute for dynamically computed sequences.

Antonio Pauletich

The Valid constraint also separates when a cascade runs from which groups are applied to the nested object. Its groups option has traditionally controlled both. Setting restrictGroups to false changes that: the listed groups trigger validation of the nested object, while its own constraints are checked against all groups being validated. This can help keep reusable objects’ validation rules from being narrowed by the parent that contains them.

Finally, Valid can now appear inside Sequentially. That supports values whose shape should be checked before Symfony cascades into them: a type constraint can run first, followed by nested validation only if the type check succeeds. The nested Valid constraint takes its groups from Sequentially, rather than defining groups of its own.

Taken together, these changes make Symfony 8.2’s validator more adaptable to typed data and nested domain models. The practical review point for upgrades is behavior: check that sequence groups, cascade triggers and decimal boundaries match the rules the application intends to enforce.

Source: Javier Eguiluz · symfony.com

From the same set