Преглед на файлове

[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
Jeremy Mikola преди 15 години
родител
ревизия
fb24b291c8
променени са 2 файла, в които са добавени 19 реда и са изтрити 0 реда
  1. 4 0
      src/Symfony/Component/Form/FieldGroup.php
  2. 15 0
      tests/Symfony/Tests/Component/Form/FieldGroupTest.php

+ 4 - 0
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);
 

+ 15 - 0
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');