浏览代码

[Form] Added method FieldInterface::isEmpty()

Bernhard Schussek 14 年之前
父节点
当前提交
1593d6f75d

+ 8 - 0
src/Symfony/Component/Form/Field.php

@@ -536,4 +536,12 @@ class Field extends Configurable implements FieldInterface
             $this->propertyPath->setValue($objectOrArray, $this->getData());
         }
     }
+
+    /**
+     * {@inheritDoc}
+     */
+    public function isEmpty()
+    {
+        return null === $this->data || '' === $this->data;
+    }
 }

+ 7 - 0
src/Symfony/Component/Form/FieldInterface.php

@@ -204,6 +204,13 @@ interface FieldInterface
      */
     function isHidden();
 
+    /**
+     * Returns whether the field is empty
+     *
+     * @return boolean
+     */
+    function isEmpty();
+
     /**
      * Sets whether this field is required to be filled out when submitted.
      *

+ 14 - 0
src/Symfony/Component/Form/Form.php

@@ -927,6 +927,20 @@ class Form extends Field implements \IteratorAggregate, FormInterface
         }
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    public function isEmpty()
+    {
+        foreach ($this->fields as $field) {
+            if (!$field->isEmpty()) {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
     /**
      * Merges two arrays without reindexing numeric keys.
      *

+ 12 - 0
src/Symfony/Component/Form/HybridField.php

@@ -123,4 +123,16 @@ class HybridField extends Form
             Field::submit($data);
         }
     }
+
+    /**
+     * {@inheritDoc}
+     */
+    public function isEmpty()
+    {
+        if ($this->mode === self::FORM) {
+            return parent::isEmpty();
+        } else {
+            return Field::isEmpty();
+        }
+    }
 }

+ 21 - 0
tests/Symfony/Tests/Component/Form/FieldTest.php

@@ -511,6 +511,27 @@ class FieldTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals($this->field, $this->field->getRoot());
     }
 
+    public function testIsEmptyReturnsTrueIfNull()
+    {
+        $this->field->setData(null);
+
+        $this->assertTrue($this->field->isEmpty());
+    }
+
+    public function testIsEmptyReturnsTrueIfEmptyString()
+    {
+        $this->field->setData('');
+
+        $this->assertTrue($this->field->isEmpty());
+    }
+
+    public function testIsEmptyReturnsFalseIfZero()
+    {
+        $this->field->setData(0);
+
+        $this->assertFalse($this->field->isEmpty());
+    }
+
     protected function createMockTransformer()
     {
         return $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface', array(), array(), '', false, false);

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

@@ -1338,6 +1338,32 @@ class FormTest extends \PHPUnit_Framework_TestCase
         $this->assertSame($ref2, $author['referenceCopy']);
     }
 
+    public function testIsEmptyReturnsTrueIfAllFieldsAreEmpty()
+    {
+        $form = new Form();
+        $field1 = new TestField('foo');
+        $field1->setData('');
+        $field2 = new TestField('bar');
+        $field2->setData(null);
+        $form->add($field1);
+        $form->add($field2);
+
+        $this->assertTrue($form->isEmpty());
+    }
+
+    public function testIsEmptyReturnsFalseIfAnyFieldIsFilled()
+    {
+        $form = new Form();
+        $field1 = new TestField('foo');
+        $field1->setData('baz');
+        $field2 = new TestField('bar');
+        $field2->setData(null);
+        $form->add($field1);
+        $form->add($field2);
+
+        $this->assertFalse($form->isEmpty());
+    }
+
     /**
      * Create a group containing two fields, "visibleField" and "hiddenField"
      *