Skip to content

[Validator] Add support for closures in the When constraint #20675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions reference/constraints/When.rst
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,12 @@ Options
``expression``
~~~~~~~~~~~~~~

**type**: ``string``
**type**: ``string|Closure``

The condition written with the expression language syntax that will be evaluated.
If the expression evaluates to a falsey value (i.e. using ``==``, not ``===``),
validation of constraints won't be triggered.
The condition as a closure or written with the expression language syntax
that will be evaluated. If the closure return value or the evaluated expression
is a falsey value (i.e. using ``==``, not ``===``), validation of constraints won't
be triggered, and constraints of the ``otherwise`` option will, if provided.

To learn more about the expression language syntax, see
:doc:`/reference/formats/expression_language`.
Expand All @@ -200,6 +201,13 @@ in your expression:

The ``context`` variable in expressions was introduced in Symfony 7.2.

When using a closure, the first argument is the object being validated.

.. versionadded:: 7.3

The support for closure in the ``expression`` option was introduced in Symfony 7.3
Copy link
Member

@xabbuh xabbuh Feb 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The support for closure in the ``expression`` option was introduced in Symfony 7.3
The support for closures in the ``expression`` option was introduced in Symfony 7.3

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done while merging. Thanks!

and requires PHP 8.5.

The ``value`` variable can be used when you want to execute more complex
validation based on its value:

Expand All @@ -215,11 +223,20 @@ validation based on its value:

class Discount
{
// either using an expression...
#[Assert\When(
expression: 'value == "percent"',
constraints: [new Assert\Callback('doComplexValidation')],
)]
// ... or using a closure
#[Assert\When(
expression: static function (Discount $discount) {
return $discount->getType() === 'percent';
},
constraints: [new Assert\Callback('doComplexValidation')],
)]
private ?string $type;

// ...

public function doComplexValidation(ExecutionContextInterface $context, $payload): void
Expand Down