From 40773bf80c94dec87f3236cc26c865c54d142e61 Mon Sep 17 00:00:00 2001 From: Stefano Torresi Date: Thu, 6 Mar 2014 11:55:55 +0100 Subject: [PATCH 1/3] add tests for #5906 --- .../ZendTest/Form/Element/CollectionTest.php | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/ZendTest/Form/Element/CollectionTest.php b/tests/ZendTest/Form/Element/CollectionTest.php index 2c75c177353..5fee2575b1e 100644 --- a/tests/ZendTest/Form/Element/CollectionTest.php +++ b/tests/ZendTest/Form/Element/CollectionTest.php @@ -784,4 +784,52 @@ public function testNestedCollections() $index++; } } + + public function testSetDataOnFormPopulatesCollection() + { + $form = new Form(); + $form->add(array( + 'name' => 'names', + 'type' => 'Collection', + 'options' => array( + 'target_element' => new Element\Text(), + ), + )); + + $names = array('foo', 'bar', 'baz', 'bat'); + + $form->setData(array( + 'names' => $names + )); + + $i = 0; + foreach($form->get('names') as $field) { + $this->assertEquals($names[$i], $field->getValue()); + $i++; + }; + } + + public function testEmptyCollectionDataReturnsSpecifiedNumberOfElementsAfterPrepare() + { + $form = new Form(); + $form->add(new Element\Text('input')); + $form->add(array( + 'name' => 'names', + 'type' => 'Collection', + 'options' => array( + 'target_element' => new Element\Text(), + 'count' => 2 + ), + )); + + $form->setData(array( + 'input' => 'foo', + )); + + $this->assertCount(0, $form->get('names')); + + $form->prepare(); + + $this->assertCount(2, $form->get('names')); + } } From f41cd45981c2cc821b4b8cf4d4b4a6c4fb2ef8ec Mon Sep 17 00:00:00 2001 From: Stefano Torresi Date: Thu, 6 Mar 2014 11:57:33 +0100 Subject: [PATCH 2/3] fix #5906 --- library/Zend/Form/Element/Collection.php | 1 - 1 file changed, 1 deletion(-) diff --git a/library/Zend/Form/Element/Collection.php b/library/Zend/Form/Element/Collection.php index 6d3a9253132..072092bd779 100644 --- a/library/Zend/Form/Element/Collection.php +++ b/library/Zend/Form/Element/Collection.php @@ -197,7 +197,6 @@ public function populateValues($data) // Can't do anything with empty data if (empty($data)) { - $this->shouldCreateChildrenOnPrepareElement = false; return; } From d8a0bef9c897d4cd3eb173de9485a0cdf3fe766b Mon Sep 17 00:00:00 2001 From: Stefano Torresi Date: Thu, 6 Mar 2014 12:09:48 +0100 Subject: [PATCH 3/3] tweaks collection tests --- tests/ZendTest/Form/Element/CollectionTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/ZendTest/Form/Element/CollectionTest.php b/tests/ZendTest/Form/Element/CollectionTest.php index 5fee2575b1e..31657146304 100644 --- a/tests/ZendTest/Form/Element/CollectionTest.php +++ b/tests/ZendTest/Form/Element/CollectionTest.php @@ -802,6 +802,8 @@ public function testSetDataOnFormPopulatesCollection() 'names' => $names )); + $this->assertCount(count($names), $form->get('names')); + $i = 0; foreach($form->get('names') as $field) { $this->assertEquals($names[$i], $field->getValue()); @@ -809,7 +811,7 @@ public function testSetDataOnFormPopulatesCollection() }; } - public function testEmptyCollectionDataReturnsSpecifiedNumberOfElementsAfterPrepare() + public function testSettingSomeDataButNoneForCollectionReturnsSpecifiedNumberOfElementsAfterPrepare() { $form = new Form(); $form->add(new Element\Text('input'));