Skip to content

Commit 03a0566

Browse files
committed
[Form] fixed more cases where the delegating validator did not match the validator paths
1 parent 1daca76 commit 03a0566

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,12 @@ private function buildDataPathMapping(FormInterface $form, array &$mapping, $dat
183183
$nestedNamePath = $namePath . '.' . $child->getName();
184184

185185
if (strpos($path, '[') === 0) {
186-
$nestedDataPath = $dataPath . $path;
186+
$nestedDataPaths = array($dataPath . $path);
187187
} else {
188-
$nestedDataPath = $dataPath . '.' . $path;
188+
$nestedDataPaths = array($dataPath . '.' . $path);
189+
if ($child->hasChildren()) {
190+
$nestedDataPaths[] = $dataPath . '[' . $path . ']';
191+
}
189192
}
190193

191194
if ($child->hasChildren()) {
@@ -195,10 +198,14 @@ private function buildDataPathMapping(FormInterface $form, array &$mapping, $dat
195198
$this->buildDataPathMapping($child, $mapping, $dataPath, $nestedNamePath);
196199
}
197200

198-
$this->buildDataPathMapping($child, $mapping, $nestedDataPath, $nestedNamePath);
201+
foreach ($nestedDataPaths as $nestedDataPath) {
202+
$this->buildDataPathMapping($child, $mapping, $nestedDataPath, $nestedNamePath);
203+
}
199204
}
200205

201-
$mapping['/^'.preg_quote($nestedDataPath, '/').'(?!\w)/'] = $child;
206+
foreach ($nestedDataPaths as $nestedDataPath) {
207+
$mapping['/^'.preg_quote($nestedDataPath, '/').'(?!\w)/'] = $child;
208+
}
202209
}
203210
}
204211

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,28 @@ public function testDataErrorsOnGrandChild2()
369369
$this->assertEquals(array($this->getFormError()), $grandChild->getErrors());
370370
}
371371

372+
public function testDataErrorsOnGrandChild3()
373+
{
374+
$parent = $this->getForm();
375+
$child = $this->getForm('address');
376+
$grandChild = $this->getForm('street');
377+
378+
$parent->add($child);
379+
$child->add($grandChild);
380+
381+
$this->delegate->expects($this->once())
382+
->method('validate')
383+
->will($this->returnValue(array(
384+
$this->getConstraintViolation('data[address].street.constrainedProp')
385+
)));
386+
387+
$this->validator->validate($parent);
388+
389+
$this->assertFalse($parent->hasErrors());
390+
$this->assertFalse($child->hasErrors());
391+
$this->assertEquals(array($this->getFormError()), $grandChild->getErrors());
392+
}
393+
372394
public function testDataErrorsOnParentIfNoChildFound()
373395
{
374396
$parent = $this->getForm();

0 commit comments

Comments
 (0)