Przeglądaj źródła

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

Bernhard Schussek 14 lat temu
rodzic
commit
681ce7f46a

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