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 all 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
10 changes: 5 additions & 5 deletions library/Zend/Form/Form.php
Expand Up @@ -634,12 +634,12 @@ protected function prepareValidationGroup(FieldsetInterface $formOrFieldset, arr
}

$value = $values;
} else {
if (!isset($data[$key])) {
$data[$key] = array();
}
$this->prepareValidationGroup($fieldset, $data[$key], $validationGroup[$key]);
}

if (!isset($data[$key])) {
$data[$key] = array();
}
$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());
}
}
82 changes: 82 additions & 0 deletions tests/ZendTest/Form/TestAsset/NestedCollectionsForm.php
@@ -0,0 +1,82 @@
<?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

/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

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'
)
),
)
));
}
}