8000 merged branch bschussek/issue4686 (PR #4828) · defrag/symfony@b260f30 · GitHub
[go: up one dir, main page]

Skip to content

Commit b260f30

Browse files
committed
merged branch bschussek/issue4686 (PR symfony#4828)
Commits ------- 854daa8 [Form] Fixed errors not to be added onto non-synchronized forms Discussion ---------- [Form] Fixed errors not to be added onto non-synchronized forms Bug fix: yes Feature addition: no Backwards compatibility break: no Symfony2 tests pass: yes Fixes the following tickets: symfony#4686 Todo: -
2 parents facbcdc + 854daa8 commit b260f30

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

src/Symfony/Component/Form/Extension/Validator/Validator/DelegatingValidator.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,18 @@ public function validate(FormInterface $form)
6666
foreach ($propertyPath->getElements() as $element) {
6767
$children = $child->getChildren();
6868
if (!isset($children[$element])) {
69-
$form->addError($error);
69+
if ($form->isSynchronized()) {
70+
$form->addError($error);
71+
}
7072
break;
7173
}
7274

7375
$child = $children[$element];
7476
}
7577

76-
$child->addError($error);
78+
if ($child->isSynchronized()) {
79+
$child->addError($error);
80+
}
7781
}
7882
}
7983
} elseif (count($violations = $this->validator->validate($form))) {
@@ -85,12 +89,16 @@ public function validate(FormInterface $form)
8589

8690
foreach ($mapping as $mappedPath => $child) {
8791
if (preg_match($mappedPath, $propertyPath)) {
88-
$child->addError($error);
92+
if ($child->isSynchronized()) {
93+
$child->addError($error);
94+
}
8995
continue 2;
9096
}
9197
}
9298

93-
$form->addError($error);
99+
if ($form->isSynchronized()) {
100+
$form->addError($error);
101+
}
94102
}
95103
}
96104
}

tests/Symfony/Tests/Component/Form/Extension/Validator/Validator/DelegatingValidatorTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
namespace Symfony\Tests\Component\Form\Extension\Validator\Validator;
1313

1414
use Symfony\Component\Form\FormBuilder;
15+
use Symfony\Component\Form\CallbackTransformer;
1516
use Symfony\Component\Form\FormError;
1617
use Symfony\Component\Form\Util\PropertyPath;
1718
use Symfony\Component\Form\Extension\Validator\Validator\DelegatingValidator;
19+
use Symfony\Component\Form\Exception\TransformationFailedException;
1820
use Symfony\Component\Validator\ConstraintViolation;
1921
use Symfony\Component\Validator\ExecutionContext;
2022

@@ -85,6 +87,24 @@ protected function getForm($name = 'name', $propertyPath = null)
8587
return $this->getBuilder($name, $propertyPath)->getForm();
8688
}
8789

90+
protected function getNonSynchronizedForm()
91+
{
92+
$form = $this->getBuilder()
93+
->appendClientTransformer(new CallbackTransformer(
94+
function ($normValue) {
95+
return $normValue;
96+
},
97+
function () {
98+
throw new TransformationFailedException('Failed');
99+
}
100+
))
101+
->getForm();
102+
103+
$form->bind('foobar');
104+
105+
return $form;
106+
}
107+
88108
protected function getMockForm()
89109
{
90110
return $this->getMock('Symfony\Tests\Component\Form\FormInterface');
@@ -118,6 +138,21 @@ public function testFormErrorsOnForm()
118138
$this->assertEquals(array($this->getFormError()), $form->getErrors());
119139
}
120140

141+
public function testNoFormErrorsOnNonSynchronizedForm()
142+
{
143+
$form = $this->getNonSynchronizedForm();
144+
145+
$this->delegate->expects($this->once())
146+
->method('validate')
147+
->will($this->returnValue(array(
148+
$this->getConstraintViolation('constrainedProp')
149+
)));
150+
151+
$this->validator->validate($form);
152+
153+
$this->assertEquals(array(), $form->getErrors());
154+
}
155+
121156
public function testFormErrorsOnChild()
122157
{
123158
$parent = $this->getForm();

0 commit comments

Comments
 (0)
0