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

Commit e2bddc6

Browse files
committed
Merge branch 'hotfix/78' into develop
Forward port #156 Conflicts: CHANGELOG.md
2 parents 5d3335b + 6143f13 commit e2bddc6

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ All notable changes to this project will be documented in this file, in reverse
2020

2121
- Nothing.
2222

23-
## 2.10.1 - TBD
23+
## 2.10.1 - 2017-04-26
2424

2525
### Added
2626

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

4954
## 2.10.0 - 2017-02-23
5055

src/Form.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,19 @@ public function attachInputFilterDefaults(InputFilterInterface $inputFilter, Fie
792792
$inputFilter->replace($input, $name);
793793
continue;
794794
}
795+
796+
// If we are dealing with a collection input filter, check
797+
// the input filter it composes for an element of the same
798+
// name as was done above.
799+
if ($inputFilter instanceof CollectionInputFilter
800+
&& $inputFilter->getInputFilter()->has($name)
801+
&& $inputFilter->getInputFilter() instanceof ReplaceableInputInterface
802+
) {
803+
$collectionInputFilter = $inputFilter->getInputFilter();
804+
$input->merge($collectionInputFilter->get($name));
805+
$collectionInputFilter->replace($input, $name);
806+
continue;
807+
}
795808
}
796809

797810
// Add element input filter to CollectionInputFilter
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-form for the canonical source repository
4+
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license https://github.com/zendframework/zend-form/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
namespace ZendTest\Form\Integration;
9+
10+
use PHPUnit\Framework\TestCase;
11+
use Zend\Form\Form;
12+
use Zend\Form\InputFilterProviderFieldset;
13+
use Zend\Validator;
14+
15+
class FormCreatesCollectionInputFilterTest extends TestCase
16+
{
17+
public static function assertValidatorFound($class, array $validators, $message = null)
18+
{
19+
$message = $message ?: sprintf('Failed to find validator of type %s in validator list', $class);
20+
foreach ($validators as $instance) {
21+
$validator = $instance['instance'];
22+
if ($validator instanceof $class) {
23+
return true;
24+
}
25+
}
26+
var_export($validators);
27+
self::fail($message);
28+
}
29+
30+
/**
31+
* @see https://github.com/zendframework/zend-form/issues/78
32+
*/
33+
public function testCollectionInputFilterContainsExpectedValidators()
34+
{
35+
$form = new Form;
36+
$form->add([
37+
'name' => 'collection',
38+
'type' => 'collection',
39+
'options' => [
40+
'target_element' => [
41+
'type' => InputFilterProviderFieldset::class,
42+
'elements' => [
43+
[
44+
'spec' => [
45+
'name' => 'date',
46+
'type' => 'date'
47+
],
48+
],
49+
],
50+
'options' => ['input_filter_spec' => [
51+
'date' => [
52+
'required' => false,
53+
'validators' => [
54+
['name' => 'StringLength'],
55+
],
56+
],
57+
]],
58+
],
59+
],
60+
]);
61+
$inputFilter = $form->getInputFilter();
62+
$filter = $inputFilter->get('collection')->getInputFilter()->get('date');
63+
64+
$validators = $filter->getValidatorChain()->getValidators();
65+
$this->assertCount(3, $validators);
66+
$this->assertValidatorFound(Validator\StringLength::class, $validators);
67+
$this->assertValidatorFound(Validator\Date::class, $validators);
68+
$this->assertValidatorFound(Validator\DateStep::class, $validators);
69+
70+
return $form;
71+
}
72+
73+
/**
74+
* @depends testCollectionInputFilterContainsExpectedValidators
75+
*/
76+
public function testCollectionElementDoesNotCreateDiscreteElementInInputFilter(Form $form)
77+
{
78+
$inputFilter = $form->getInputFilter();
79+
$this->assertFalse($inputFilter->has('date'));
80+
}
81+
}

0 commit comments

Comments
 (0)