10000 [Validator] Pass context to expressions used in `When` constraints by KoNekoD · Pull Request #58512 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Pass context to expressions used in When constraints #58512

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
Oct 10, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
Loading
0