Skip to content

Commit

Permalink
Merge branch 'feature/5106' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Oct 22, 2013
2 parents e029955 + 3d4cd83 commit c23c250
Show file tree
Hide file tree
Showing 4 changed files with 321 additions and 0 deletions.
4 changes: 4 additions & 0 deletions library/Zend/InputFilter/BaseInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,10 @@ protected function populate()
// No value; clear value in this input
if ($input instanceof InputFilterInterface) {
$input->setData(array());
if ($input instanceof CollectionInputFilter) {
$input->clearValues();
$input->clearRawValues();
}
continue;
}

Expand Down
26 changes: 26 additions & 0 deletions library/Zend/InputFilter/CollectionInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ public function isValid()
$valid = false;
}

if (empty($this->collectionData)) {
$this->clearValues();
$this->clearRawValues();
return $valid;
}

$inputs = $this->validationGroup ?: array_keys($this->inputs);
foreach ($this->collectionData as $key => $data) {
if (!is_array($data)) {
Expand Down Expand Up @@ -254,6 +260,26 @@ public function getRawValues()
return $this->collectionRawValues;
}

/**
* Clear collectionValues
*
* @access public
*/
public function clearValues()
{
return $this->collectionValues = array();
}

/**
* Clear collectionRawValues
*
* @access public
*/
public function clearRawValues()
{
return $this->collectionRawValues = array();
}

/**
* {@inheritdoc}
*/
Expand Down
141 changes: 141 additions & 0 deletions tests/ZendTest/Form/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1702,4 +1702,145 @@ public function testInputFilterNotAddedTwiceWhenUsingFieldsets()
$filters = $form->getInputFilter()->get('fieldset')->get('foo')->getFilterChain();
$this->assertEquals(1, $filters->count());
}

public function testFormWithNestedCollections()
{
$spec = array(
'name' => 'test',
'elements' => array(
array(
'spec' => array(
'name' => 'name',
'type' => 'Zend\Form\Element\Text',
),
'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' => 'group_class',
),
),
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' => 'id',
),
),
array(
'spec' => array(
'type' => 'Zend\Form\Element\Text',
'name' => 'type',
),
),
),
),
),
),
),
),
),
),
)
)
),
'input_filter' => array(
'type' => 'Zend\InputFilter\InputFilter',
'name' => array(
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'Null'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'max' => 255,
),
),
),
),
'groups' => array(
'type' => 'Zend\InputFilter\CollectionInputFilter',
'input_filter' => array(
'type' => 'Zend\InputFilter\InputFilter',
'group_class' => array(
'required' => false,
),
'items' => array(
'type' => 'Zend\InputFilter\CollectionInputFilter',
'input_filter' => array(
'type' => 'Zend\InputFilter\InputFilter',
'id' => array(
'required' => false,
),
'type' => array(
'required' => false,
),
),
),
),
),
),
);

$factory = new Factory();
$this->form = $factory->createForm($spec);

$data = array(
'name' => 'foo',
'groups' => array(
array(
'group_class' => 'bar',
'items' => array(
array(
'id' => 100,
'type' => 'item-1',
),
),
),
array(
'group_class' => 'bar',
'items' => array(
array(
'id' => 200,
'type' => 'item-2',
),
array(
'id' => 300,
'type' => 'item-3',
),
array(
'id' => 400,
'type' => 'item-4',
),
),
),
array(
'group_class' => 'biz',
'items' => array(),
),
),
);

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

$isValid = $this->form->isValid();
$this->assertEquals($data, $this->form->getData());
}
}
150 changes: 150 additions & 0 deletions tests/ZendTest/InputFilter/CollectionInputFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,4 +391,154 @@ public function testSetRequired()
$this->filter->setIsRequired(true);
$this->assertEquals(true,$this->filter->getIsRequired());
}

public function testNestedCollectionWithEmptyChild()
{
$items_inputfilter = new BaseInputFilter();
$items_inputfilter->add(new Input(), 'id')
->add(new Input(), 'type');
$items = new CollectionInputFilter();
$items->setInputFilter($items_inputfilter);

$groups_inputfilter = new BaseInputFilter();
$groups_inputfilter->add(new Input(), 'group_class')
->add($items, 'items');
$groups = new CollectionInputFilter();
$groups->setInputFilter($groups_inputfilter);

$inputFilter = new BaseInputFilter();
$inputFilter->add($groups, 'groups');

$preFilterdata = array(
'groups' => array(
array(
'group_class' => 'bar',
'items' => array(
array(
'id' => 100,
'type' => 'item-1',
),
),
),
array(
'group_class' => 'bar',
'items' => array(
array(
'id' => 200,
'type' => 'item-2',
),
array(
'id' => 300,
'type' => 'item-3',
),
array(
'id' => 400,
'type' => 'item-4',
),
),
),
array(
'group_class' => 'biz',
),
),
);

$postFilterdata = array(
'groups' => array(
array(
'group_class' => 'bar',
'items' => array(
array(
'id' => 100,
'type' => 'item-1',
),
),
),
array(
'group_class' => 'bar',
'items' => array(
array(
'id' => 200,
'type' => 'item-2',
),
array(
'id' => 300,
'type' => 'item-3',
),
array(
'id' => 400,
'type' => 'item-4',
),
),
),
array(
'group_class' => 'biz',
'items' => array(),
),
),
);

$inputFilter->setData($preFilterdata);
$inputFilter->isValid();
$values = $inputFilter->getValues();
$this->assertEquals($postFilterdata, $values);
}

public function testNestedCollectionWithEmptyData()
{
$items_inputfilter = new BaseInputFilter();
$items_inputfilter->add(new Input(), 'id')
->add(new Input(), 'type');
$items = new CollectionInputFilter();
$items->setInputFilter($items_inputfilter);

$groups_inputfilter = new BaseInputFilter();
$groups_inputfilter->add(new Input(), 'group_class')
->add($items, 'items');
$groups = new CollectionInputFilter();
$groups->setInputFilter($groups_inputfilter);

$inputFilter = new BaseInputFilter();
$inputFilter->add($groups, 'groups');

$data = array(
'groups' => array(
array(
'group_class' => 'bar',
'items' => array(
array(
'id' => 100,
'type' => 'item-1',
),
),
),
array(
'group_class' => 'biz',
'items' => array(),
),
array(
'group_class' => 'bar',
'items' => array(
array(
'id' => 200,
'type' => 'item-2',
),
array(
'id' => 300,
'type' => 'item-3',
),
array(
'id' => 400,
'type' => 'item-4',
),
),
),
),
);

$inputFilter->setData($data);
$inputFilter->isValid();
$values = $inputFilter->getValues();
$this->assertEquals($data, $values);
}
}

0 comments on commit c23c250

Please sign in to comment.