Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2bf1b37

Browse files
committed
[Validator] Fixed ExpressionValidator when the validation root is not an object
1 parentef6f5f5 commit2bf1b37

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 =newPropertyPath($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 =newPropertyPath($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+
publicfunctiontestSucceedingExpressionAtPropertyLevelWithoutRoot()
203+
{
204+
$constraint =newExpression('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+
publicfunctiontestFailingExpressionAtPropertyLevelWithoutRoot()
229+
{
230+
$constraint =newExpression(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)

[8]ページ先頭

©2009-2025 Movatter.jp