From f05ce633a9a31f7a49aab33d2564165c38a361be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=9F=A6=85KoNekoD?= Date: Wed, 9 Oct 2024 15:09:40 +0300 Subject: [PATCH] [Validator] Pass context to expressions used in `When` constraints --- src/Symfony/Component/Validator/CHANGELOG.md | 1 + .../Validator/Constraints/WhenValidator.php | 1 + .../Tests/Constraints/WhenValidatorTest.php | 25 +++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index b9af46e493aa9..6b5be184c0101 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -12,6 +12,7 @@ CHANGELOG * Add the `WordCount` constraint * Add the `Week` constraint * Add `CompoundConstraintTestCase` to ease testing Compound Constraints + * Add context variable to `WhenValidator` 7.1 --- diff --git a/src/Symfony/Component/Validator/Constraints/WhenValidator.php b/src/Symfony/Component/Validator/Constraints/WhenValidator.php index c02a450e8c399..b41ba83ff48df 100644 --- a/src/Symfony/Component/Validator/Constraints/WhenValidator.php +++ b/src/Symfony/Component/Validator/Constraints/WhenValidator.php @@ -33,6 +33,7 @@ public function validate(mixed $value, Constraint $constraint): void $variables = $constraint->values; $variables['value'] = $value; $variables['this'] = $context->getObject(); + $variables['context'] = $context; if ($this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) { $context->getValidator()->inContext($context) diff --git a/src/Symfony/Component/Validator/Tests/Constraints/WhenValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/WhenValidatorTest.php index cba81b25af34a..f79d530319b44 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/WhenValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/WhenValidatorTest.php @@ -85,6 +85,31 @@ public function testConstraintsAreExecutedWithObject() ])); } + public function testConstraintsAreExecutedWithNestedObject() + { + $parent = new \stdClass(); + $parent->child = new \stdClass(); + $parent->ok = true; + + $number = new \stdClass(); + $number->value = 1; + + $this->setObject($parent); + $this->setPropertyPath('child.value'); + $this->setRoot($parent); + + $constraints = [ + new PositiveOrZero(), + ]; + + $this->expectValidateValue(0, $number->value, $constraints); + + $this->validator->validate($number->value, new When([ + 'expression' => 'context.getRoot().ok === true', + 'constraints' => $constraints, + ])); + } + public function testConstraintsAreExecutedWithValue() { $constraints = [ pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy