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

Commit

Permalink
Merge dad31e9 into a2c828e
Browse files Browse the repository at this point in the history
  • Loading branch information
werequireevenmoreminerals committed Feb 21, 2019
2 parents a2c828e + dad31e9 commit 07ef367
Show file tree
Hide file tree
Showing 3 changed files with 44 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 !== null) {
if ($elementOrFieldset instanceof FieldsetInterface) {
$elementOrFieldset->populateValues($value);
} else {
$elementOrFieldset->setAttribute('value', $value);
}
}
}

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

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

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

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

$form->setData($data);
$form->isValid();

// expect the fieldsets key to be an empty array since there's no valid targetElement
$this->assertEquals(
[
'fieldsets' => []
],
$form->getData()
);
}
}

0 comments on commit 07ef367

Please sign in to comment.