From 1593d6f75deab634e6e31208ac0ee89f942a01e6 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Wed, 16 Feb 2011 10:28:33 +0100 Subject: [PATCH] [Form] Added method FieldInterface::isEmpty() --- src/Symfony/Component/Form/Field.php | 8 ++++++ src/Symfony/Component/Form/FieldInterface.php | 7 +++++ src/Symfony/Component/Form/Form.php | 14 ++++++++++ src/Symfony/Component/Form/HybridField.php | 12 +++++++++ .../Tests/Component/Form/FieldTest.php | 21 +++++++++++++++ .../Symfony/Tests/Component/Form/FormTest.php | 26 +++++++++++++++++++ 6 files changed, 88 insertions(+) diff --git a/src/Symfony/Component/Form/Field.php b/src/Symfony/Component/Form/Field.php index 95c2b907a905..25e65339a6cd 100644 --- a/src/Symfony/Component/Form/Field.php +++ b/src/Symfony/Component/Form/Field.php @@ -536,4 +536,12 @@ public function writeProperty(&$objectOrArray) $this->propertyPath->setValue($objectOrArray, $this->getData()); } } + + /** + * {@inheritDoc} + */ + public function isEmpty() + { + return null === $this->data || '' === $this->data; + } } diff --git a/src/Symfony/Component/Form/FieldInterface.php b/src/Symfony/Component/Form/FieldInterface.php index c0c3680f86cd..205d0bd13a33 100644 --- a/src/Symfony/Component/Form/FieldInterface.php +++ b/src/Symfony/Component/Form/FieldInterface.php @@ -204,6 +204,13 @@ function isDisabled(); */ function isHidden(); + /** + * Returns whether the field is empty + * + * @return boolean + */ + function isEmpty(); + /** * Sets whether this field is required to be filled out when submitted. * diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index b847af8bf226..d8a6e38174b3 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/src/Symfony/Component/Form/Form.php @@ -927,6 +927,20 @@ public function writeProperty(&$objectOrArray) } } + /** + * {@inheritDoc} + */ + public function isEmpty() + { + foreach ($this->fields as $field) { + if (!$field->isEmpty()) { + return false; + } + } + + return true; + } + /** * Merges two arrays without reindexing numeric keys. * diff --git a/src/Symfony/Component/Form/HybridField.php b/src/Symfony/Component/Form/HybridField.php index d794fb1901e7..7c097c06b2c1 100644 --- a/src/Symfony/Component/Form/HybridField.php +++ b/src/Symfony/Component/Form/HybridField.php @@ -123,4 +123,16 @@ public function submit($data) Field::submit($data); } } + + /** + * {@inheritDoc} + */ + public function isEmpty() + { + if ($this->mode === self::FORM) { + return parent::isEmpty(); + } else { + return Field::isEmpty(); + } + } } \ No newline at end of file diff --git a/tests/Symfony/Tests/Component/Form/FieldTest.php b/tests/Symfony/Tests/Component/Form/FieldTest.php index 91fe28fea052..f37d66969865 100644 --- a/tests/Symfony/Tests/Component/Form/FieldTest.php +++ b/tests/Symfony/Tests/Component/Form/FieldTest.php @@ -511,6 +511,27 @@ public function testGetRootReturnsFieldIfNoParent() $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); diff --git a/tests/Symfony/Tests/Component/Form/FormTest.php b/tests/Symfony/Tests/Component/Form/FormTest.php index 0ca3a20cfd9e..1a614069f3e5 100644 --- a/tests/Symfony/Tests/Component/Form/FormTest.php +++ b/tests/Symfony/Tests/Component/Form/FormTest.php @@ -1338,6 +1338,32 @@ public function testSubformAlwaysInsertsIntoArrays() $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" *