瀏覽代碼

Merge remote branch 'kriswallsmith/form/is-valid-read-only'

* kriswallsmith/form/is-valid-read-only:
  [Form] fixed isValid() on readOnly forms that have children
Fabien Potencier 14 年之前
父節點
當前提交
4da7909f9a
共有 2 個文件被更改,包括 21 次插入3 次删除
  1. 5 3
      src/Symfony/Component/Form/Form.php
  2. 16 0
      tests/Symfony/Tests/Component/Form/FormTest.php

+ 5 - 3
src/Symfony/Component/Form/Form.php

@@ -679,10 +679,12 @@ class Form implements \IteratorAggregate, FormInterface
             return false;
         }
 
-        foreach ($this->children as $child) {
-            if (!$child->isValid()) {
+        if (!$this->readOnly) {
+            foreach ($this->children as $child) {
+                if (!$child->isValid()) {
 
-                return false;
+                    return false;
+                }
             }
         }
 

+ 16 - 0
tests/Symfony/Tests/Component/Form/FormTest.php

@@ -306,6 +306,22 @@ class FormTest extends \PHPUnit_Framework_TestCase
         $this->assertTrue($form->isValid());
     }
 
+    public function testValidIfBoundAndReadOnlyWithChildren()
+    {
+        $this->factory->expects($this->once())
+            ->method('createNamedBuilder')
+            ->with('text', 'name', null, array())
+            ->will($this->returnValue($this->getBuilder('name')));
+
+        $form = $this->getBuilder('person')
+            ->setReadOnly(true)
+            ->add('name', 'text')
+            ->getForm();
+        $form->bind(array('name' => 'Jacques Doe'));
+
+        $this->assertTrue($form->isValid());
+    }
+
     public function testNotValidIfNotBound()
     {
         $this->assertFalse($this->form->isValid());