New in Symfony 7.2: Constraint Improvements
November 8, 2024 • Published by Javier Eguiluz
Symfony 7.2 is backed by:
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.
1 2 3 4 5 6 7 8 9 10 11 12
// src/Entity/Transaction.php
namespace App\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class Transaction
{
#[Assert\Bic(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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// An example DTO class used as the items of the collection
class MetadataInput
{
public $key;
public $value;
// ...
}
class Input
{
/** @var MetadataInput[] */
#[Unique(
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:
1 2 3 4 5 6 7 8 9
use Symfony\Component\Uid\Ulid;
$ulid = Ulid::fromString('01E439TP9XJZ9RPFH3T1PYBCR8');
$ulid->toBinary(); // string(16) "\x01\x71\x06\x9d\x59\x3d\x97\xd3\x8b\x3e\x23\xd0\x6d\xe5\xb3\x08"
$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:
1 2 3 4 5 6 7 8 9 10
// src/Entity/File.php
namespace App\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class File
{
#[Assert\Ulid(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:
1 2 3 4 5 6 7 8 9 10
use Symfony\Component\Validator\Constraints as Assert;
// ...
#[Assert\When(
expression: 'this.getType() == "percent" && context.getRoot().ok === true',
constraints: [
// ...
],
)]
private ?int $value;
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.