New in Symfony 7.2: Constraint Improvements
In Symfony 7.2, besides introducing three new constraints and
improving the Compound constraint, we’ve also improved other constraints.
Added a Validation Mode for BIC Constraint
The BIC constraint validates that a given value is a valid Business Identifier Code (BIC).
The BIC code requires letters to be in uppercase, but people sometimes input them
in lowercase. Instead of normalizing the given value automatically before validating
it, in Symfony 7.2 we’re introducing two validation modes:
strict (this is the default mode): validates the given value without changing it;
case-insensitive: changes the case of the given value to uppercase before validating it.
// src/Entity/Transaction.php
namespace AppEntity;
use SymfonyComponentValidatorConstraints as Assert;
class Transaction
{
(mode: ‚case-insensitive‘)
protected string $businessIdentifierCode;
// …
}
Add errorPath to Unique Constraint
The Unique constraint validates that all the elements of a given collection
are unique within that collection. However, when a validation error happens, the
error message is displayed for the entire element, instead of some specific item
field. In Symfony 7.2 we’re improving that thanks to the new errorPath option.
In the following example, the validation errors will be displayed in the key
value of each collection item:
// An example DTO class used as the items of the collection
class MetadataInput
{
public $key;
public $value;
// …
}
class Input
{
/** @var MetadataInput[] */
(
normalizer: [MetadataInput::class, ‚getKeyForUniqueConstraint‘],
errorPath: ‚key‘
)
public $metadata;
}
Add format to Ulid Constraint
When using the UID component to generate ULIDs, you can convert ULID values
into different formats:
use SymfonyComponentUidUlid;
$ulid = Ulid::fromString(’01E439TP9XJZ9RPFH3T1PYBCR8′);
$ulid->toBinary(); // string(16) „x01x71x06x9dx59x3dx97xd3x8bx3ex23xd0x6dxe5xb3x08“
$ulid->toBase32(); // string(26) „01E439TP9XJZ9RPFH3T1PYBCR8“
$ulid->toBase58(); // string(22) „1BKocMc5BnrVcuq2ti4Eqm“
$ulid->toRfc4122(); // string(36) „0171069d-593d-97d3-8b3e-23d06de5b308“
$ulid->toHex(); // string(34) „0x0171069d593d97d38b3e23d06de5b308“
However, when using the Ulid constraint to validate those ULIDs, you can only
pass values in the base32 format (Ulid::FORMAT_BASE_32). In Symfony 7.2
we’re improving this thanks to a new format option:
// src/Entity/File.php
namespace AppEntity;
use SymfonyComponentValidatorConstraints as Assert;
class File
{
(format: Ulid::FORMAT_RFC4122)
protected string $identifier;
}
Add Context to When Constraint
The When constraint allows applying constraints only if the given expression
returns true. In Symfony 7.2 we’re improving it by passing the context to
the expression, so you can make decisions based on it. Inside the expression,
the context is available via the context variable:
use SymfonyComponentValidatorConstraints as Assert;
// …
expression: ‚this.getType() == „percent“ && context.getRoot().ok === true‘,
constraints: [
// …
],
)
private ?int $value;
Symfony Blog