From fb24b291c826c55497b2daa1d56a978de5425146 Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Thu, 15 Jul 2010 11:36:13 -0400 Subject: [PATCH] [Form] FieldGroup::addError() can now map errors to fields within nested FieldGroups Property paths such as fields[group].fields[innerGroup].data were not being resolved correctly, since the second iteration of addError() (based on "group") would attempt to call get('fields') instead of get('innerGroup'). Solution is to remember to bump the propertyPath forward if we're at the fields property --- src/Symfony/Component/Form/FieldGroup.php | 4 ++++ .../Tests/Component/Form/FieldGroupTest.php | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/Symfony/Component/Form/FieldGroup.php b/src/Symfony/Component/Form/FieldGroup.php index b7abb4fa60..ba5d9a0df9 100644 --- a/src/Symfony/Component/Form/FieldGroup.php +++ b/src/Symfony/Component/Form/FieldGroup.php @@ -373,6 +373,10 @@ class FieldGroup extends Field implements \IteratorAggregate, FieldGroupInterfac if ($type === self::FIELD_ERROR && $path->hasNext()) { $path->next(); + if ($path->isProperty() && $path->getCurrent() === 'fields') { + $path->next(); + } + if ($this->has($path->getCurrent()) && !$this->get($path->getCurrent())->isHidden()) { $this->get($path->getCurrent())->addError($message, $path, $type); diff --git a/tests/Symfony/Tests/Component/Form/FieldGroupTest.php b/tests/Symfony/Tests/Component/Form/FieldGroupTest.php index 7a9957aa4b..af0c3c4dc8 100644 --- a/tests/Symfony/Tests/Component/Form/FieldGroupTest.php +++ b/tests/Symfony/Tests/Component/Form/FieldGroupTest.php @@ -156,6 +156,21 @@ class FieldGroupTest extends \PHPUnit_Framework_TestCase $group->addError('Message', new PropertyPath('fields[firstName].data'), FieldGroup::FIELD_ERROR); } + public function testAddErrorMapsFieldValidationErrorsOntoFieldsWithinNestedFieldGroups() + { + $field = $this->createMockField('firstName'); + $field->expects($this->once()) + ->method('addError') + ->with($this->equalTo('Message')); + + $group = new FieldGroup('author'); + $innerGroup = new FieldGroup('names'); + $innerGroup->add($field); + $group->add($innerGroup); + + $group->addError('Message', new PropertyPath('fields[names].fields[firstName].data'), FieldGroup::FIELD_ERROR); + } + public function testAddErrorKeepsFieldValidationErrorsIfFieldNotFound() { $field = $this->createMockField('foo');