Skip to content

Commit

Permalink
Merge branch '2.8' into 3.2
Browse files Browse the repository at this point in the history
* 2.8:
  [ClassLoader] Use only forward slashes in generated class map
  ensure the proper context for nested validations
  bug #20653 [WebProfilerBundle] Profiler includes ghost panels
  • Loading branch information
fabpot committed Nov 29, 2016
2 parents a25d3c8 + 18cc10e commit b640393
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Context/ExecutionContext.php
Expand Up @@ -269,6 +269,11 @@ public function getGroup()
return $this->group;
}

public function getConstraint()
{
return $this->constraint;
}

/**
* {@inheritdoc}
*/
Expand Down
19 changes: 19 additions & 0 deletions Tests/Validator/AbstractTest.php
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Validator\Tests\Validator;

use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Traverse;
Expand Down Expand Up @@ -651,4 +652,22 @@ public function testPassConstraintToViolation()
$this->assertCount(1, $violations);
$this->assertSame($constraint, $violations[0]->getConstraint());
}

public function testCollectionConstraitViolationHasCorrectContext()
{
$data = array(
'foo' => 'fooValue',
);

// Missing field must not be the first in the collection validation
$constraint = new Collection(array(
'foo' => new NotNull(),
'bar' => new NotNull(),
));

$violations = $this->validate($data, $constraint);

$this->assertCount(1, $violations);
$this->assertSame($constraint, $violations[0]->getConstraint());
}
}
10 changes: 10 additions & 0 deletions Validator/RecursiveContextualValidator.php
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
use Symfony\Component\Validator\Context\ExecutionContext;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\Exception\NoSuchMetadataException;
Expand Down Expand Up @@ -110,6 +111,11 @@ public function validate($value, $constraints = null, $groups = null)
$previousMetadata = $this->context->getMetadata();
$previousPath = $this->context->getPropertyPath();
$previousGroup = $this->context->getGroup();
$previousConstraint = null;

if ($this->context instanceof ExecutionContext || method_exists($this->context, 'getConstraint')) {
$previousConstraint = $this->context->getConstraint();
}

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

if (null !== $previousConstraint) {
$this->context->setConstraint($previousConstraint);
}

return $this;
}

Expand Down

0 comments on commit b640393

Please sign in to comment.