Skip to content

Commit d16278f

Browse files
committed
[Form] Fixed check of violation constraint #12792
ConstraintViolation::getConstraint() must not expect to provide a constraint as long as Symfony\Component\Validator\ExecutionContext exists (before 3.0)
1 parent 69525e4 commit d16278f

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

Extension/Validator/EventListener/ValidationListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public function validateForm(FormEvent $event)
6666
foreach ($violations as $violation) {
6767
// Allow the "invalid" constraint to be put onto
6868
// non-synchronized forms
69-
$allowNonSynchronized = $violation->getConstraint() instanceof Form && Form::NOT_SYNCHRONIZED_ERROR === $violation->getCode();
69+
// ConstraintViolation::getConstraint() must not expect to provide a constraint as long as Symfony\Component\Validator\ExecutionContext exists (before 3.0)
70+
$allowNonSynchronized = (null === $violation->getConstraint() || $violation->getConstraint() instanceof Form) && Form::NOT_SYNCHRONIZED_ERROR === $violation->getCode();
7071

7172
$this->violationMapper->mapViolation($violation, $form, $allowNonSynchronized);
7273
}

Tests/Extension/Validator/EventListener/ValidationListenerTest.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ protected function setUp()
6464
$this->params = array('foo' => 'bar');
6565
}
6666

67-
private function getConstraintViolation($code = null)
67+
private function getConstraintViolation($code = null, $constraint = null)
6868
{
69-
return new ConstraintViolation($this->message, $this->messageTemplate, $this->params, null, 'prop.path', null, null, $code, new Form());
69+
return new ConstraintViolation($this->message, $this->messageTemplate, $this->params, null, 'prop.path', null, null, $code, $constraint);
7070
}
7171

7272
private function getBuilder($name = 'name', $propertyPath = null, $dataClass = null)
@@ -93,7 +93,7 @@ private function getMockForm()
9393
// More specific mapping tests can be found in ViolationMapperTest
9494
public function testMapViolation()
9595
{
96-
$violation = $this->getConstraintViolation();
96+
$violation = $this->getConstraintViolation(null, new Form());
9797
$form = $this->getForm('street');
9898

9999
$this->validator->expects($this->once())
@@ -109,7 +109,28 @@ public function testMapViolation()
109109

110110
public function testMapViolationAllowsNonSyncIfInvalid()
111111
{
112-
$violation = $this->getConstraintViolation(Form::NOT_SYNCHRONIZED_ERROR);
112+
$violation = $this->getConstraintViolation(Form::NOT_SYNCHRONIZED_ERROR, new Form());
113+
$form = $this->getForm('street');
114+
115+
$this->validator->expects($this->once())
116+
->method('validate')
117+
->will($this->returnValue(array($violation)));
118+
119+
$this->violationMapper->expects($this->once())
120+
->method('mapViolation')
121+
// pass true now
122+
->with($violation, $form, true);
123+
124+
$this->listener->validateForm(new FormEvent($form, null));
125+
}
126+
127+
public function testMapViolationAllowsNonSyncIfInvalidWithoutConstraintReference()
128+
{
129+
// constraint violations have no reference to the constraint if they are created by
130+
// Symfony\Component\Validator\ExecutionContext
131+
// which is deprecated in favor of
132+
// Symfony\Component\Validator\Context\ExecutionContext
133+
$violation = $this->getConstraintViolation(Form::NOT_SYNCHRONIZED_ERROR, null);
113134
$form = $this->getForm('street');
114135

115136
$this->validator->expects($this->once())

0 commit comments

Comments
 (0)