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

Fix how inputs for elements nested in collections are merged #156

Closed
wants to merge 3 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,19 @@ public function attachInputFilterDefaults(InputFilterInterface $inputFilter, Fie
$inputFilter->replace($input, $name);
continue;
}

// If we are dealing with a collection input filter, check
// the input filter it composes for an element of the same
// name as was done above.
if ($inputFilter instanceof CollectionInputFilter
&& $inputFilter->getInputFilter()->has($name)
&& $inputFilter->getInputFilter() instanceof ReplaceableInputInterface
) {
$collectionInputFilter = $inputFilter->getInputFilter();
$input->merge($collectionInputFilter->get($name));
$collectionInputFilter->replace($input, $name);
continue;
}
}

// Add element input filter to CollectionInputFilter
Expand Down
81 changes: 81 additions & 0 deletions test/Integration/FormCreatesCollectionInputFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* @see https://github.com/zendframework/zend-form for the canonical source repository
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-form/blob/master/LICENSE.md New BSD License
*/

namespace ZendTest\Form\Integration;

use PHPUnit_Framework_TestCase as TestCase;
use Zend\Form\Form;
use Zend\Form\InputFilterProviderFieldset;
use Zend\Validator;

class FormCreatesCollectionInputFilterTest extends TestCase
{
public static function assertValidatorFound($class, array $validators, $message = null)
{
$message = $message ?: sprintf('Failed to find validator of type %s in validator list', $class);
foreach ($validators as $instance) {
$validator = $instance['instance'];
if ($validator instanceof $class) {
return true;
}
}
var_export($validators);
self::fail($message);
}

/**
* @see https://github.com/zendframework/zend-form/issues/78
*/
public function testCollectionInputFilterContainsExpectedValidators()
{
$form = new Form;
$form->add([
'name' => 'collection',
'type' => 'collection',
'options' => [
'target_element' => [
'type' => InputFilterProviderFieldset::class,
'elements' => [
[
'spec' => [
'name' => 'date',
'type' => 'date'
],
],
],
'options' => ['input_filter_spec' => [
'date' => [
'required' => false,
'validators' => [
['name' => 'StringLength'],
],
],
]],
],
],
]);
$inputFilter = $form->getInputFilter();
$filter = $inputFilter->get('collection')->getInputFilter()->get('date');

$validators = $filter->getValidatorChain()->getValidators();
$this->assertCount(3, $validators);
$this->assertValidatorFound(Validator\StringLength::class, $validators);
$this->assertValidatorFound(Validator\Date::class, $validators);
$this->assertValidatorFound(Validator\DateStep::class, $validators);

return $form;
}

/**
* @depends testCollectionInputFilterContainsExpectedValidators
*/
public function testCollectionElementDoesNotCreateDiscreteElementInInputFilter(Form $form)
{
$inputFilter = $form->getInputFilter();
$this->assertFalse($inputFilter->has('date'));
}
}