Permalink
Browse files

Merge pull request #5923 from stefanotorresi/fix/5906-collection-coun…

…t-is-ignored-when-data-empty

Fix/5906 collection count is ignored when data empty
  • Loading branch information...
2 parents b06a6c7 + d8a0bef commit bef84436924207bad47b771d0b2da5803f69c2af @weierophinney weierophinney committed Mar 7, 2014
Showing with 50 additions and 1 deletion.
  1. +0 −1 library/Zend/Form/Element/Collection.php
  2. +50 −0 tests/ZendTest/Form/Element/CollectionTest.php
@@ -197,7 +197,6 @@ public function populateValues($data)
// Can't do anything with empty data
if (empty($data)) {
- $this->shouldCreateChildrenOnPrepareElement = false;
return;
}
@@ -823,4 +823,54 @@ 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
+ ));
+
+ $this->assertCount(count($names), $form->get('names'));
+
+ $i = 0;
+ foreach($form->get('names') as $field) {
+ $this->assertEquals($names[$i], $field->getValue());
+ $i++;
+ };
+ }
+
+ public function testSettingSomeDataButNoneForCollectionReturnsSpecifiedNumberOfElementsAfterPrepare()
+ {
+ $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'));
+ }
}

0 comments on commit bef8443

Please sign in to comment.