symfony/symfony — Issue #3543: CollectionType() does not remove elements when bound with empty data
Author: greg0ire
Created: 2012-03-09 16:33:17+00:00
State: open

Someone asked a question about this behavior on SO : http://stackoverflow.com/questions/9637137/bind-and-save-a-form-with-a-collection-field-using-an-array-doctrine2, and I think this is a bug
I wrote a test that fails, here is a patch :

```
diff --git a/tests/Symfony/Tests/Component/Form/Extension/Core/Type/CollectionTypeTest.php b/tests/Symfony/Tests/Component/Form/Extension/Core/Type/CollectionTypeTest.php
index 3c237ba..4336e35 100644
--- a/tests/Symfony/Tests/Component/Form/Extension/Core/Type/CollectionTypeTest.php
+++ b/tests/Symfony/Tests/Component/Form/Extension/Core/Type/CollectionTypeTest.php
@@ -88,6 +88,20 @@ class CollectionFormTest extends TypeTestCase
$this->assertEquals(array('foo@bar.com'), $form->getData());
}

+ public function testResizedDownIfBoundWithEmptyDataAndAllowDelete()
+ {
+ $form = $this->factory->create('collection', null, array(
+ 'type' => 'field',
+ 'allow_delete' => true,
+ ));
+ $form->setData(array('foo@foo.com', 'bar@bar.com'));
+ $form->bind(array(''));
+
+ $this->assertFalse($form->has('0'));
+ $this->assertFalse($form->has('1'));
+ $this->assertEquals(array(''), $form->getData());
+ }
+
public function testNotResizedIfBoundWithExtraData()
{
$form = $this->factory->create('collection', null, array(

```