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

Commit56c8ff8

Browse files
committed
ensure the proper context for nested validations
1 parente62b602 commit56c8ff8

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

‎src/Symfony/Component/Validator/Context/ExecutionContext.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,11 @@ public function getGroup()
287287
return$this->group;
288288
}
289289

290+
publicfunctiongetConstraint()
291+
{
292+
return$this->constraint;
293+
}
294+
290295
/**
291296
* {@inheritdoc}
292297
*/

‎src/Symfony/Component/Validator/Tests/Validator/Abstract2Dot5ApiTest.php‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespaceSymfony\Component\Validator\Tests\Validator;
1313

1414
useSymfony\Component\Validator\Constraints\Callback;
15+
useSymfony\Component\Validator\Constraints\Collection;
1516
useSymfony\Component\Validator\Constraints\GroupSequence;
1617
useSymfony\Component\Validator\Constraints\NotNull;
1718
useSymfony\Component\Validator\Constraints\Traverse;
@@ -720,4 +721,22 @@ public function testPassConstraintToViolation()
720721
$this->assertCount(1,$violations);
721722
$this->assertSame($constraint,$violations[0]->getConstraint());
722723
}
724+
725+
publicfunctiontestCollectionConstraitViolationHasCorrectContext()
726+
{
727+
$data =array(
728+
'foo' =>'fooValue',
729+
);
730+
731+
// Missing field must not be the first in the collection validation
732+
$constraint =newCollection(array(
733+
'foo' =>newNotNull(),
734+
'bar' =>newNotNull(),
735+
));
736+
737+
$violations =$this->validate($data,$constraint);
738+
739+
$this->assertCount(1,$violations);
740+
$this->assertSame($constraint,$violations[0]->getConstraint());
741+
}
723742
}

‎src/Symfony/Component/Validator/Validator/RecursiveContextualValidator.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
useSymfony\Component\Validator\Constraint;
1515
useSymfony\Component\Validator\Constraints\GroupSequence;
1616
useSymfony\Component\Validator\ConstraintValidatorFactoryInterface;
17+
useSymfony\Component\Validator\Context\ExecutionContext;
1718
useSymfony\Component\Validator\Context\ExecutionContextInterface;
1819
useSymfony\Component\Validator\Exception\ConstraintDefinitionException;
1920
useSymfony\Component\Validator\Exception\NoSuchMetadataException;
@@ -110,6 +111,11 @@ public function validate($value, $constraints = null, $groups = null)
110111
$previousMetadata =$this->context->getMetadata();
111112
$previousPath =$this->context->getPropertyPath();
112113
$previousGroup =$this->context->getGroup();
114+
$previousConstraint =null;
115+
116+
if ($this->contextinstanceof ExecutionContext ||method_exists($this->context,'getConstraint')) {
117+
$previousConstraint =$this->context->getConstraint();
118+
}
113119

114120
// If explicit constraints are passed, validate the value against
115121
// those constraints
@@ -138,6 +144,10 @@ public function validate($value, $constraints = null, $groups = null)
138144
$this->context->setNode($previousValue,$previousObject,$previousMetadata,$previousPath);
139145
$this->context->setGroup($previousGroup);
140146

147+
if (null !==$previousConstraint) {
148+
$this->context->setConstraint($previousConstraint);
149+
}
150+
141151
return$this;
142152
}
143153

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp