8000 [Validator] Fixed ExpressionValidator when the validation root is not… · symfony/symfony@2bf1b37 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2bf1b37

Browse files
committed
[Validator] Fixed ExpressionValidator when the valid 8000 ation root is not an object
1 parent ef6f5f5 commit 2bf1b37

File tree

2 files changed

+66
-6
lines changed

2 files changed

+66
-6
lines changed

src/Symfony/Component/Validator/Constraints/ExpressionValidator.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,18 @@ public function validate($value, Constraint $constraint)
7474
$variables['value'] = $value;
7575
$variables['this'] = $value;
7676
} else {
77-
// Extract the object that the property belongs to from the object
78-
// graph
79-
$path = new PropertyPath($this->context->getPropertyPath());
80-
$parentPath = $path->getParent();
8177
$root = $this->context->getRoot();
82-
8378
$variables['value'] = $value;
84-
$variables['this'] = $parentPath ? $this->getPropertyAccessor()->getValue($root, $parentPath) : $root;
79+
80+
if (is_object($root)) {
81+
// Extract the object that the property belongs to from the object
82+
// graph
83+
$path = new PropertyPath($this->context->getPropertyPath());
84+
$parentPath = $path->getParent();
85+
$variables['this'] = $parentPath ? $this->getPropertyAccessor()->getValue($root, $parentPath) : $root;
86+
} else {
87+
$variables['this'] = null;
88+
}
8589
}
8690

8791
if (!$this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) {

src/Symfony/Component/Validator/Tests/Constraints/ExpressionValidatorTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,60 @@ public function testFailingExpressionAtNestedPropertyLevel()
194194

195195
$this->validator->validate('2', $constraint);
196196
}
197+
198+
/**
199+
* When validatePropertyValue() is called with a class name
200+
* https://github.com/symfony/symfony/pull/11498
201+
*/
202+
public function testSucceedingExpressionAtPropertyLevelWithoutRoot()
203+
{
204+
$constraint = new Expression('value == "1"');
205+
206+
$this->context->expects($this->any())
207+
->method('getPropertyName')
208+
->will($this->returnValue('property'));
209+
210+
$this->context->expects($this->any())
211+
->method('getPropertyPath')
212+
->will($this->returnValue(''));
213+
214+
$this->context->expects($this->any())
215+
->method('getRoot')
216+
->will($this->returnValue('1'));
217+
218+
$this->context->expects($this->never())
219+
->method('addViolation');
220+
221+
$this->validator->validate('1', $constraint);
222+
}
223+
224+
/**
225+
* When validatePropertyValue() is called with a class name
226+
* https://github.com/symfony/symfony/pull/11498
227+
*/
228+
public function testFailingExpressionAtPropertyLevelWithoutRoot()
229+
{
230+
$constraint = new Expression(array(
231+
'expression' => 'value == "1"',
232+
'message' => 'myMessage',
233+
));
234+
235+
$this->context->expects($this->any())
236+
->method('getPropertyName')
237+
->will($this->returnValue('property'));
238+
239+
$this->context->expects($this->any())
240+
->method('getPropertyPath')
241+
->will($this->returnValue(''));
242+
243+
$this->context->expects($this->any())
244+
->method('getRoot')
245+
->will($this->returnValue('2'));
246+
247+
$this->context->expects($this->once())
248+
->method('addViolation')
249+
->with('myMessage');
250+
251+
$this->validator->validate('2', $constraint);
252+
}
197253
}

0 commit comments

Comments
 (0)
0