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

Commit

Permalink
Merge branch 'hotfix/7426' into develop
Browse files Browse the repository at this point in the history
Forward port #7426
  • Loading branch information
weierophinney committed May 4, 2015
2 parents 1810382 + 2c5f68c commit a867770
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 57 deletions.
52 changes: 49 additions & 3 deletions library/Zend/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,12 @@ public function attachInputFilterDefaults(InputFilterInterface $inputFilter, Fie
}
}

$inputFilter->add($input, $name);
// Add element input filter to CollectionInputFilter
if ($inputFilter instanceof CollectionInputFilter && !$inputFilter->getInputFilter()->has($name)) {
$inputFilter->getInputFilter()->add($input, $name);
} else {
$inputFilter->add($input, $name);
}
}

if ($fieldset === $this && $fieldset instanceof InputFilterProviderInterface) {
Expand All @@ -807,8 +812,27 @@ public function attachInputFilterDefaults(InputFilterInterface $inputFilter, Fie
if ($childFieldset->getObject() instanceof InputFilterAwareInterface) {
$inputFilter->add($childFieldset->getObject()->getInputFilter(), $name);
} else {
if ($fieldset instanceof Collection && $inputFilter instanceof CollectionInputFilter) {
continue;
// Add input filter for collections via getInputFilterSpecification()
if ($childFieldset instanceof Collection
&& $childFieldset->getTargetElement() instanceof InputFilterProviderInterface
&& $childFieldset->getTargetElement()->getInputFilterSpecification()
) {
$collectionContainerFilter = new CollectionInputFilter();

$spec = $childFieldset->getTargetElement()->getInputFilterSpecification();
$filter = $inputFactory->createInputFilter($spec);

$collectionContainerFilter->setInputFilter($filter);

$inputFilter->add($collectionContainerFilter, $name);

// We need to copy the inputs to the collection input filter
if ($inputFilter instanceof CollectionInputFilter) {
$inputFilter = $this->addInputsToCollectionInputFilter($inputFilter);
}

// Add child elements from target element
$childFieldset = $childFieldset->getTargetElement();
} else {
$inputFilter->add(new InputFilter(), $name);
}
Expand Down Expand Up @@ -840,9 +864,31 @@ public function attachInputFilterDefaults(InputFilterInterface $inputFilter, Fie

// Recursively attach sub filters
$this->attachInputFilterDefaults($filter, $childFieldset);

// We need to copy the inputs to the collection input filter to ensure that all sub filters are added
if ($inputFilter instanceof CollectionInputFilter) {
$inputFilter = $this->addInputsToCollectionInputFilter($inputFilter);
}
}
}

/**
* Add inputs to CollectionInputFilter
*
* @param CollectionInputFilter $inputFilter
* @return CollectionInputFilter
*/
private function addInputsToCollectionInputFilter(CollectionInputFilter $inputFilter)
{
foreach ($inputFilter->getInputs() as $name => $input) {
if (!$inputFilter->getInputFilter()->has($name)) {
$inputFilter->getInputFilter()->add($input, $name);
}
}

return $inputFilter;
}

/**
* Are the form elements/fieldsets names wrapped by the form name ?
*
Expand Down
25 changes: 24 additions & 1 deletion tests/ZendTest/Form/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1257,7 +1257,7 @@ public function testDoNotApplyEmptyInputFiltersToSubFieldsetOfCollectionElements
$this->form->setInputFilter($inputFilter);

$this->assertInstanceOf('Zend\InputFilter\CollectionInputFilter', $this->form->getInputFilter()->get('items'));
$this->assertCount(1, $this->form->getInputFilter()->get('items')->getInputs());
$this->assertCount(1, $this->form->getInputFilter()->get('items')->getInputFilter()->getInputs());
}

public function testFormValidationCanHandleNonConsecutiveKeysOfCollectionInData()
Expand Down Expand Up @@ -1876,6 +1876,29 @@ public function testFormWithNestedCollections()
$this->assertEquals($data, $this->form->getData());
}

public function testFormWithCollectionsAndNestedFieldsetsWithInputFilterProviderInterface()
{
$this->form->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'nested_fieldset_with_input_filter_provider',
'options' => array(
'label' => 'InputFilterProviderFieldset',
'count' => 1,
'target_element' => array(
'type' => 'ZendTest\Form\TestAsset\InputFilterProviderFieldset'
)
),
));

$this->assertTrue(
$this->form->getInputFilter()
->get('nested_fieldset_with_input_filter_provider')
->getInputFilter()
->get('foo')
instanceof \Zend\InputFilter\Input
);
}

public function testFormElementValidatorsMergeIntoAppliedInputFilter()
{
$this->form->add(array(
Expand Down
39 changes: 39 additions & 0 deletions tests/ZendTest/Form/TestAsset/InputFilterProviderFieldset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 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\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;

class InputFilterProviderFieldset extends Fieldset implements InputFilterProviderInterface
{
public function __construct($name = null, $options = array())
{
parent::__construct($name, $options);

$this->add(array(
'name' => 'foo',
'options' => array(
'label' => 'Foo'
),
));

$this->add(new BasicFieldset());
}

public function getInputFilterSpecification()
{
return array(
'foo' => array(
'required' => true,
)
);
}
}
53 changes: 0 additions & 53 deletions tests/ZendTest/Mvc/Controller/Plugin/FilePostRedirectGetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,59 +274,6 @@ public function testReuseMatchedParametersWithSegmentController()
$this->assertEquals(303, $prgResultRoute->getStatusCode());
}

public function testFieldsetAmountInFormEqualsFieldsetsInInputFilter()
{
// POST
$url = '/';
$params = array(
'links' => array(
'0' => array(
'foobar' => 'val',
),
'1' => array(
'foobar' => 'val',
),
),
);
$this->request->setMethod('POST');
$this->request->setPost(new Parameters($params));
$this->request->setUri($url);

$this->form->add($this->collection);

$routeMatch = $this->event->getRouter()->match($this->request);
$this->event->setRouteMatch($routeMatch);

$this->controller->dispatch($this->request, $this->response);
$prgResultUrl = $this->controller->fileprg($this->form);

$this->assertInstanceOf('Zend\Http\Response', $prgResultUrl);
$this->assertTrue($prgResultUrl->getHeaders()->has('Location'));
$this->assertEquals('/', $prgResultUrl->getHeaders()->get('Location')->getUri());
$this->assertEquals(303, $prgResultUrl->getStatusCode());

$this->assertCount(count($params['links']), $this->form->get('links')->getFieldsets());
$this->assertCount(count($this->form->get('links')->getFieldsets()), $this->form->getInputFilter()->get('links')->getInputs());

// GET
$this->request = new Request();
$form = new Form();
$collection = new Collection('links', array(
'count' => 1,
'allow_add' => true,
'target_element' => array(
'type' => 'ZendTest\Mvc\Controller\Plugin\TestAsset\LinksFieldset',
),
));
$form->add($collection);
$this->controller->dispatch($this->request, $this->response);
$prgResult = $this->controller->fileprg($form);

$this->assertEquals($params, $prgResult);
$this->assertCount(count($params['links']), $form->get('links')->getFieldsets());
$this->assertCount(count($form->get('links')->getFieldsets()), $form->getInputFilter()->get('links')->getInputs());
}

public function testCollectionInputFilterIsInitializedBeforePluginRetrievesIt()
{
$fieldset = new TestAsset\InputFilterProviderFieldset();
Expand Down

0 comments on commit a867770

Please sign in to comment.