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

Fix / tests for #6363 #6400

Merged
merged 6 commits into from Aug 7, 2014
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions library/Zend/Form/Form.php
Expand Up @@ -638,6 +638,9 @@ protected function prepareValidationGroup(FieldsetInterface $formOrFieldset, arr
if (!isset($data[$key])) {
$data[$key] = array();
}
}

if(is_array($data[$key])) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I came across this issue myself today and just moving everything inside the else block to outside fixed the issue for me...if you do that the is_array check won't be necessary.

$this->prepareValidationGroup($fieldset, $data[$key], $validationGroup[$key]);
}
}
Expand Down
99 changes: 99 additions & 0 deletions tests/ZendTest/Form/FormTest.php
Expand Up @@ -2008,4 +2008,103 @@ public function testCanSetUseInputFilterDefaultsViaArray()
$this->form = $factory->createForm($spec);
$this->assertFalse($this->form->useInputFilterDefaults());
}



/**
* Error test for https://github.com/zendframework/zf2/issues/6363 comment #1
*/
public function testSetValidationGroupOnFormWithNestedCollectionsRaisesInvalidArgumentException()
{
$this->form = new TestAsset\NestedCollectionsForm;

$data = array(
'testFieldset' => array(
'groups' => array(
array(
'name' => 'first',
'items' => array(
array(
'itemId' => 1,
),
array(
'itemId' => 2,
),
),
),
array(
'name' => 'second',
'items' => array(
array(
'itemId' => 3,
),
),
),
array(
'name' => 'third',
'items' => array(),
),
),
),
);

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

$this->assertEquals($data, $this->form->getData());
}


/**
* Test for https://github.com/zendframework/zf2/issues/6363 comment #2
*/
public function testSetValidationGroupOnFormWithNestedCollectionsPopulatesOnlyFirstNestedCollectionElement()
{
$this->form = new TestAsset\NestedCollectionsForm;

$data = array(
'testFieldset' => array(
'groups' => array(
array(
'name' => 'first',
'items' => array(
array(
'itemId' => 1,
),
array(
'itemId' => 2,
),
),
),
array(
'name' => 'second',
'items' => array(
array(
'itemId' => 3,
),
array(
'itemId' => 4,
),
),
),
array(
'name' => 'third',
'items' => array(
array(
'itemId' => 5,
),
array(
'itemId' => 6,
),
),
),
),
),
);

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

$this->assertEquals($data, $this->form->getData());
}
}
78 changes: 78 additions & 0 deletions tests/ZendTest/Form/TestAsset/NestedCollectionsForm.php
@@ -0,0 +1,78 @@
<?php
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

License header here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samsonasik I've just updated the file. Thank you

namespace ZendTest\Form\TestAsset;

use Zend\Form\Form;
use Zend\Form\Fieldset;

class NestedCollectionsForm extends Form
{

public function __construct()
{
parent::__construct('nestedCollectionsForm');

$this->add(array(
'name' => 'testFieldset',
'type' => 'Zend\Form\Fieldset',
'options' => array(
'use_as_base_fieldset' => true,
),
'elements' => array(
array(
'spec' => array(
'name' => 'groups',
'type' => 'Zend\Form\Element\Collection',
'options' => array(
'target_element' => array(
'type' => 'Zend\Form\Fieldset',
'name' => 'group',
'elements' => array(
array(
'spec' => array(
'type' => 'Zend\Form\Element\Text',
'name' => 'name',
),
),
array(
'spec' => array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'items',
'options' => array(
'target_element' => array(
'type' => 'Zend\Form\Fieldset',
'name' => 'item',
'elements' => array(
array(
'spec' => array(
'type' => 'Zend\Form\Element\Text',
'name' => 'itemId',
),
),
),
),
),
),
),
),
),
),
),
),
),
));


$this->setValidationGroup(array(
'testFieldset' => array(
'groups' => array(
'name',
'items' => array(
'itemId'
)
),
)
));
}


}