Skip to content

Commit ec937cb

Browse files
committed
bug symfony#20664 [Validator] ensure the proper context for nested validations (xabbuh)
This PR was merged into the 2.7 branch. Discussion ---------- [Validator] ensure the proper context for nested validations | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#14639, symfony#20368 | License | MIT | Doc PR | n/a Commits ------- 56c8ff8 ensure the proper context for nested validations
2 parents 6d01ad2 + 56c8ff8 commit ec937cb

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+
public function getConstraint()
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
namespace Symfony\Component\Validator\Tests\Validator;
1313

1414
use Symfony\Component\Validator\Constraints\Callback;
15+
use Symfony\Component\Validator\Constraints\Collection;
1516
use Symfony\Component\Validator\Constraints\GroupSequence;
1617
use Symfony\Component\Validator\Constraints\NotNull;
1718
use Symfony\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+
public function testCollectionConstraitViolationHasCorrectContext()
726+
{
727+
$data = array(
728+
'foo' => 'fooValue',
729+
);
730+
731+
// Missing field must not be the first in the collection validation
732+
$constraint = new Collection(array(
733+
'foo' => new NotNull(),
734+
'bar' => new NotNull(),
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
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\Constraints\GroupSequence;
1616
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
17+
use Symfony\Component\Validator\Context\ExecutionContext;
1718
use Symfony\Component\Validator\Context\ExecutionContextInterface;
1819
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
1920
use Symfony\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->context instanceof 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)