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

Commit

Permalink
Merge branch 'hotfix/78' into develop
Browse files Browse the repository at this point in the history
Forward port #156

Conflicts:
	CHANGELOG.md
  • Loading branch information
weierophinney committed Apr 26, 2017
2 parents 5d3335b + 6143f13 commit e2bddc6
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ All notable changes to this project will be documented in this file, in reverse

- Nothing.

## 2.10.1 - TBD
## 2.10.1 - 2017-04-26

### Added

Expand All @@ -45,6 +45,11 @@ All notable changes to this project will be documented in this file, in reverse
- [#136](https://github.com/zendframework/zend-form/pull/136) fixes how error
messages are provided when an element uses a required `ArrayInput`, but no
values are submitted. Previously, no messages were returned; now they are.
- [#156](https://github.com/zendframework/zend-form/pull/156) fixes how elements
that act as `InputProvider`s are merged into parent `CollectionInputFilter`s;
previously, forms did not check if the element was in the target input filter
composed in a `CollectionInputFilter`, leading to duplicate elements with
varying behavior; now the inputs are correctly merged.

## 2.10.0 - 2017-02-23

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

0 comments on commit e2bddc6

Please sign in to comment.