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

Cannot validate form when keys of collection in data are non consecutive #3828

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 2 additions & 4 deletions library/Zend/Form/Form.php
Expand Up @@ -558,10 +558,8 @@ protected function prepareValidationGroup(FieldsetInterface $formOrFieldset, arr
$values = array();

if (isset($data[$key])) {
$count = count($data[$key]);

for ($i = 0; $i != $count; ++$i) {
$values[] = $value;
foreach(array_keys($data[$key]) as $cKey) {
$values[$cKey] = $value;
}
}

Expand Down
32 changes: 32 additions & 0 deletions tests/ZendTest/Form/FormTest.php
Expand Up @@ -1124,6 +1124,38 @@ public function testApplyObjectInputFilterToBaseFieldsetAndApplyValidationGroup(
$this->assertTrue($this->form->isValid());
}

public function testFormValidationCanHandleNonConsecutiveKeysOfCollectionInData()
{
$dataWithCollection = array(
'foo' => 'bar',
'categories' => array(
0 => array('name' => 'cat1'),
1 => array('name' => 'cat2'),
3 => array('name' => 'cat3'),
),
);
$this->populateForm();
$this->form->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'categories',
'options' => array(
'count' => 1,
'allow_add' => true,
'target_element' => array(
'type' => 'ZendTest\Form\TestAsset\CategoryFieldset'
)
)
));
$this->form->setValidationGroup(array(
'foo',
'categories' => array(
'name'
)
));
$this->form->setData($dataWithCollection);
$this->assertTrue($this->form->isValid());
}

public function testAddNonBaseFieldsetObjectInputFilterToFormInputFilter()
{
$fieldset = new Fieldset('foobar');
Expand Down