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

Commit ff9385b

Browse files
committed
Merge branch 'hotfix/230'
Close #230
2 parents 7fde0e8 + 2193919 commit ff9385b

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5-
## 2.14.1 - TBD
5+
## 2.14.1 - 2019-02-26
66

77
### Added
88

@@ -22,7 +22,9 @@ All notable changes to this project will be documented in this file, in reverse
2222

2323
### Fixed
2424

25-
- Nothing.
25+
- [#230](https://github.com/zendframework/zend-form/pull/230) fixes the "`__clone` method called on non-object" error that happens when
26+
the `$targetElement` is `null` within a `Collection` instance. It now properly
27+
sets the data to an empty array in such circumstances.
2628

2729
## 2.14.0 - 2019-01-07
2830

docs/book/quick-start.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,9 @@ behaviors with the shipped annotations in zend-form:
10071007
of validator options for the constructor:
10081008
`@Validator({"name": "StringLength", "options": {"min":3, "max": 25}})`.
10091009
This annotation may be specified multiple times.
1010+
- `ContinueIfEmpty`: indicate whether the element can be submitted when it
1011+
is empty. A boolean value is expected. If `@Required` is set to `false`, this
1012+
needs to be set to `true` to allow the field to be empty.
10101013

10111014
To use annotations, include them in your class and/or property docblocks.
10121015
Annotation names will be resolved according to the import statements in your

src/Element/Collection.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,10 @@ public function populateValues($data)
225225
}
226226

227227
foreach ($data as $key => $value) {
228+
$elementOrFieldset = null;
228229
if ($this->has($key)) {
229230
$elementOrFieldset = $this->get($key);
230-
} else {
231+
} elseif ($this->targetElement) {
231232
$elementOrFieldset = $this->addNewTargetElementInstance($key);
232233

233234
if ($key > $this->lastChildIndex) {
@@ -237,7 +238,10 @@ public function populateValues($data)
237238

238239
if ($elementOrFieldset instanceof FieldsetInterface) {
239240
$elementOrFieldset->populateValues($value);
240-
} else {
241+
continue;
242+
}
243+
244+
if ($elementOrFieldset !== null) {
241245
$elementOrFieldset->setAttribute('value', $value);
242246
}
243247
}

test/Element/CollectionTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,4 +1398,40 @@ public function testGetErrorMessagesForInvalidCollectionElements()
13981398
$this->form->getMessages()
13991399
);
14001400
}
1401+
1402+
/**
1403+
* @see https://github.com/zendframework/zend-form/pull/230
1404+
*/
1405+
public function testNullTargetElementShouldResultInEmptyData()
1406+
{
1407+
$form = new Form();
1408+
1409+
$form->add([
1410+
'type' => \Zend\Form\Element\Collection::class,
1411+
'name' => 'fieldsets',
1412+
'options' => [
1413+
'count' => 2,
1414+
],
1415+
]);
1416+
1417+
$collection = $form->get('fieldsets');
1418+
$data = [
1419+
'fieldsets' => [
1420+
'red',
1421+
'green',
1422+
'blue',
1423+
],
1424+
];
1425+
1426+
$form->setData($data);
1427+
$form->isValid();
1428+
1429+
// expect the fieldsets key to be an empty array since there's no valid targetElement
1430+
$this->assertEquals(
1431+
[
1432+
'fieldsets' => [],
1433+
],
1434+
$form->getData()
1435+
);
1436+
}
14011437
}

0 commit comments

Comments
 (0)