Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge ce463ae into a2c828e
Browse files Browse the repository at this point in the history
  • Loading branch information
werequireevenmoreminerals committed Feb 21, 2019
2 parents a2c828e + ce463ae commit 475e5a9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -22,7 +22,7 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- Nothing.
- [#230](https://github.com/zendframework/zend-form/pull/230) fixes the "__clone method called on non-object" error that happens when the targetElement is null.

## 2.14.0 - 2019-01-07

Expand Down
13 changes: 8 additions & 5 deletions src/Element/Collection.php
Expand Up @@ -225,20 +225,23 @@ public function populateValues($data)
}

foreach ($data as $key => $value) {
$elementOrFieldset = null;
if ($this->has($key)) {
$elementOrFieldset = $this->get($key);
} else {
} elseif ($this->targetElement) {
$elementOrFieldset = $this->addNewTargetElementInstance($key);

if ($key > $this->lastChildIndex) {
$this->lastChildIndex = $key;
}
}

if ($elementOrFieldset instanceof FieldsetInterface) {
$elementOrFieldset->populateValues($value);
} else {
$elementOrFieldset->setAttribute('value', $value);
if ($elementOrFieldset) {
if ($elementOrFieldset instanceof FieldsetInterface) {
$elementOrFieldset->populateValues($value);
} else {
$elementOrFieldset->setAttribute('value', $value);
}
}
}

Expand Down
21 changes: 21 additions & 0 deletions test/Element/CollectionTest.php
Expand Up @@ -1398,4 +1398,25 @@ public function testGetErrorMessagesForInvalidCollectionElements()
$this->form->getMessages()
);
}

public function testTargetElementBeingNullNotCausingAnError()
{
$form = new Form();

$form->add([
'type' => 'Zend\Form\Element\Collection',
'name' => 'fieldsets',
'options' => [
'count' => 2
]
]);

$collection = $form->get('fieldsets');
$data = [];
$data['fieldsets'] = ['red', 'green', 'blue'];

$form->populateValues($data);

$this->addToAssertionCount(1); // expect no exception being thrown
}
}

0 comments on commit 475e5a9

Please sign in to comment.