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

Commit 2a09ba6

Browse files
committed
Merge pull request #19 from svycka/hotfix/does-not-hydrate-empty-collection
Fix collection hydration problem
2 parents 06ddda1 + 82b0cdc commit 2a09ba6

File tree

4 files changed

+65
-13
lines changed

4 files changed

+65
-13
lines changed

src/Fieldset.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Traversable;
1313
use Zend\Code\Reflection\ClassReflection;
14+
use Zend\Form\Element\Collection;
1415
use Zend\Hydrator;
1516
use Zend\Hydrator\HydratorAwareInterface;
1617
use Zend\Hydrator\HydratorInterface;
@@ -564,12 +565,18 @@ public function bindValues(array $values = [])
564565
$hydrator = $this->getHydrator();
565566
$hydratableData = [];
566567

567-
foreach ($values as $name => $value) {
568-
if (!$this->has($name)) {
569-
continue;
568+
foreach ($this->iterator as $element) {
569+
$name = $element->getName();
570+
571+
if (!array_key_exists($name, $values)) {
572+
if (!($element instanceof Collection)) {
573+
continue;
574+
}
575+
576+
$values[$name] = [];
570577
}
571578

572-
$element = $this->iterator->get($name);
579+
$value = $values[$name];
573580

574581
if ($element instanceof FieldsetInterface && $element->allowValueBinding()) {
575582
$value = $element->bindValues($value);

test/Element/CollectionTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -527,17 +527,17 @@ public function testAddingCollectionElementAfterBind()
527527
*/
528528
public function testDoesNotCreateNewObjectsWhenUsingNestedCollections()
529529
{
530-
$addressesFieldeset = new \ZendTest\Form\TestAsset\AddressFieldset();
531-
$addressesFieldeset->setHydrator(new \Zend\Hydrator\ClassMethods());
532-
$addressesFieldeset->remove('city');
530+
$addressesFieldset = new \ZendTest\Form\TestAsset\AddressFieldset();
531+
$addressesFieldset->setHydrator(new \Zend\Hydrator\ClassMethods());
532+
$addressesFieldset->remove('city');
533533

534534
$form = new Form();
535535
$form->setHydrator(new ObjectPropertyHydrator());
536536
$form->add([
537537
'name' => 'addresses',
538538
'type' => 'Collection',
539539
'options' => [
540-
'target_element' => $addressesFieldeset,
540+
'target_element' => $addressesFieldset,
541541
'count' => 1
542542
],
543543
]);

test/FormTest.php

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,10 +377,10 @@ public function testCanAddFileEnctypeFromCollectionAttribute()
377377

378378
$fileCollection = new Element\Collection('collection');
379379
$fileCollection->setOptions([
380-
'count' => 2,
381-
'allow_add' => false,
382-
'allow_remove' => false,
383-
'target_element' => $file,
380+
'count' => 2,
381+
'allow_add' => false,
382+
'allow_remove' => false,
383+
'target_element' => $file,
384384
]);
385385
$this->form->add($fileCollection);
386386

@@ -2158,4 +2158,39 @@ public function testGetInputFilterInjectsFormInputFilterFactoryInstanceWhenObjec
21582158
$inputFilter = $this->form->getInputFilter();
21592159
$this->assertSame($inputFilterFactory, $inputFilter->getFactory());
21602160
}
2161+
2162+
public function testShouldHydrateEmptyCollection()
2163+
{
2164+
$fieldset = new Fieldset('example');
2165+
$fieldset->add([
2166+
'type' => 'Zend\Form\Element\Collection',
2167+
'name' => 'foo',
2168+
'options' => [
2169+
'label' => 'InputFilterProviderFieldset',
2170+
'count' => 1,
2171+
'target_element' => [
2172+
'type' => 'text'
2173+
]
2174+
],
2175+
]);
2176+
2177+
$this->form->add($fieldset);
2178+
$this->form->setBaseFieldset($fieldset);
2179+
$this->form->setHydrator(new \Zend\Hydrator\ObjectProperty());
2180+
2181+
$object = new Entity\SimplePublicProperty();
2182+
$object->foo = ['item 1', 'item 2'];
2183+
2184+
$this->form->bind($object);
2185+
2186+
$this->form->setData([
2187+
'submit' => 'Confirm',
2188+
'example' => [
2189+
//'foo' => [] // $_POST does't have this if collection is empty
2190+
]
2191+
]);
2192+
2193+
$this->assertTrue($this->form->isValid());
2194+
$this->assertEquals([], $this->form->getObject()->foo);
2195+
}
21612196
}

test/TestAsset/PhoneFieldset.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
use Zend\Form\Fieldset;
1313
use Zend\Form\Element;
1414
use Zend\Hydrator\ClassMethods as ClassMethodsHydrator;
15+
use Zend\InputFilter\InputFilterProviderInterface;
1516
use ZendTest\Form\TestAsset\Entity\Phone;
1617

17-
class PhoneFieldset extends Fieldset
18+
class PhoneFieldset extends Fieldset implements InputFilterProviderInterface
1819
{
1920
public function __construct()
2021
{
@@ -31,4 +32,13 @@ public function __construct()
3132
->setAttribute('class', 'form-control');
3233
$this->add($number);
3334
}
35+
36+
public function getInputFilterSpecification()
37+
{
38+
return [
39+
'number' => [
40+
'required' => true,
41+
]
42+
];
43+
}
3444
}

0 commit comments

Comments
 (0)