Skip to content

Commit

Permalink
[Form] Added method FieldInterface::isEmpty()
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernhard Schussek authored and fabpot committed Feb 16, 2011
1 parent f589304 commit 1593d6f
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Symfony/Component/Form/Field.php
Expand Up @@ -536,4 +536,12 @@ public function writeProperty(&$objectOrArray)
$this->propertyPath->setValue($objectOrArray, $this->getData());
}
}

/**
* {@inheritDoc}
*/
public function isEmpty()
{
return null === $this->data || '' === $this->data;
}
}
7 changes: 7 additions & 0 deletions src/Symfony/Component/Form/FieldInterface.php
Expand Up @@ -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.
*
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/Form/Form.php
Expand Up @@ -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.
*
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/Form/HybridField.php
Expand Up @@ -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();
}
}
}
21 changes: 21 additions & 0 deletions tests/Symfony/Tests/Component/Form/FieldTest.php
Expand Up @@ -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);
Expand Down
26 changes: 26 additions & 0 deletions tests/Symfony/Tests/Component/Form/FormTest.php
Expand Up @@ -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"
*
Expand Down

0 comments on commit 1593d6f

Please sign in to comment.