Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Validator/Context/ExecutionContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ public function getGroup()
return $this->group;
}

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

/**
* {@inheritdoc}
*/
Expand Down
Original file line number Diff line number Diff line change
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 @@ -720,4 +721,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());
}
}
Original file line number Diff line number Diff line change
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')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just if (method_exists($this->context, 'getConstraint')) ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not test it, but the instanceof check should be faster and should be true most of the time.

$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