10BC0 [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
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
---
8000 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
8000
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