浏览代码

[Form] Fixed: FieldGroup::hasErrors() does not return true if only children have errors

Bernhard Schussek 14 年之前
父节点
当前提交
681ce7f46a
共有 2 个文件被更改,包括 15 次插入1 次删除
  1. 5 1
      src/Symfony/Component/Form/Field.php
  2. 10 0
      tests/Symfony/Tests/Component/Form/FieldGroupTest.php

+ 5 - 1
src/Symfony/Component/Form/Field.php

@@ -349,7 +349,11 @@ abstract class Field extends Configurable implements FieldInterface
      */
     public function hasErrors()
     {
-        return $this->isBound() && !$this->isValid();
+        // Don't call isValid() here, as its semantics are slightly different
+        // Field groups are not valid if their children are invalid, but
+        // hasErrors() returns only true if a field/field group itself has
+        // errors
+        return count($this->errors) > 0;
     }
 
     /**

+ 10 - 0
tests/Symfony/Tests/Component/Form/FieldGroupTest.php

@@ -119,6 +119,16 @@ class FieldGroupTest extends \PHPUnit_Framework_TestCase
         $this->assertTrue($group->isBoundWithExtraFields());
     }
 
+    public function testHasNoErrorsIfOnlyFieldHasErrors()
+    {
+        $group = new TestFieldGroup('author');
+        $group->add($this->createInvalidMockField('firstName'));
+
+        $group->bind(array('firstName' => 'Bernhard'));
+
+        $this->assertFalse($group->hasErrors());
+    }
+
     public function testBindForwardsBoundValues()
     {
         $field = $this->createMockField('firstName');