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
Contributed by
Max Beckers
in
#54879
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 App\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class Transaction
{
#[Assert\Bic(mode: 'case-insensitive')]
protected string $businessIdentifierCode;
// ...
}
Add errorPath
to Unique
Constraint
Contributed by
Tomas Norkūnas
in
#57436
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[] */
#[Unique(
normalizer: [MetadataInput::class, 'getKeyForUniqueConstraint'],
errorPath: 'key'
)]
public $metadata;
}
Contributed by
Alexandre Daubois
in
#57438
and #57938
When using the UID component to generate ULIDs, you can convert ULID values
into different formats:
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:
// 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
Contributed by
KoNekoD
in
#58512
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 Symfony\Component\Validator\Constraints as Assert;
// ...
#[Assert\When(
expression: 'this.getType() == "percent" && context.getRoot().ok === true',
constraints: [
// ...
],
)]
private ?int $value;
Sponsor the Symfony project.