From 7dbb1662a62f8accbeb04bb5d088b9ad0d08358d Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Wed, 26 Apr 2017 12:14:21 -0500 Subject: [PATCH 1/3] Created failing test case --- .../FormCreatesCollectionInputFilterTest.php | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 test/Integration/FormCreatesCollectionInputFilterTest.php diff --git a/test/Integration/FormCreatesCollectionInputFilterTest.php b/test/Integration/FormCreatesCollectionInputFilterTest.php new file mode 100644 index 00000000..cb98d4bd --- /dev/null +++ b/test/Integration/FormCreatesCollectionInputFilterTest.php @@ -0,0 +1,68 @@ +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\Step::class, $validators); + } +} From 5239951aabbe426e1929cd0c4eecd8a46b253d90 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Wed, 26 Apr 2017 12:16:26 -0500 Subject: [PATCH 2/3] Added test case asserting generated input filter does not contain extra element --- .../FormCreatesCollectionInputFilterTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/Integration/FormCreatesCollectionInputFilterTest.php b/test/Integration/FormCreatesCollectionInputFilterTest.php index cb98d4bd..308b71bf 100644 --- a/test/Integration/FormCreatesCollectionInputFilterTest.php +++ b/test/Integration/FormCreatesCollectionInputFilterTest.php @@ -64,5 +64,16 @@ public function testCollectionInputFilterContainsExpectedValidators() $this->assertValidatorFound(Validator\StringLength::class, $validators); $this->assertValidatorFound(Validator\Date::class, $validators); $this->assertValidatorFound(Validator\Step::class, $validators); + + return $form; + } + + /** + * @depends testCollectionInputFilterContainsExpectedValidators + */ + public function testCollectionElementDoesNotCreateDiscreteElementInInputFilter(Form $form) + { + $inputFilter = $form->getInputFilter(); + $this->assertFalse($inputFilter->has('date')); } } From 2a0dfe57529e4a3a2c102555931d0e68896780ec Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Wed, 26 Apr 2017 15:06:33 -0500 Subject: [PATCH 3/3] Ensure input-provider elements of collections merge inputs correctly As reported in #78, if you compose an input filter provider fieldset as a target element of a collection in order to allow providing a default fieldset for the collection, input provider elements composed by the input filter provider fieldset were not being merged correctly into the input filter. This patch checks to see if the fieldset's input filter represents a collection, and, if so, will check its own composed input filter for the element and merge it if found. --- src/Form.php | 13 +++++++++++++ .../FormCreatesCollectionInputFilterTest.php | 6 ++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Form.php b/src/Form.php index 960b7b92..c9c1a43f 100644 --- a/src/Form.php +++ b/src/Form.php @@ -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 diff --git a/test/Integration/FormCreatesCollectionInputFilterTest.php b/test/Integration/FormCreatesCollectionInputFilterTest.php index 308b71bf..0e893ec7 100644 --- a/test/Integration/FormCreatesCollectionInputFilterTest.php +++ b/test/Integration/FormCreatesCollectionInputFilterTest.php @@ -17,11 +17,13 @@ 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 $validator) { + foreach ($validators as $instance) { + $validator = $instance['instance']; if ($validator instanceof $class) { return true; } } + var_export($validators); self::fail($message); } @@ -63,7 +65,7 @@ public function testCollectionInputFilterContainsExpectedValidators() $this->assertCount(3, $validators); $this->assertValidatorFound(Validator\StringLength::class, $validators); $this->assertValidatorFound(Validator\Date::class, $validators); - $this->assertValidatorFound(Validator\Step::class, $validators); + $this->assertValidatorFound(Validator\DateStep::class, $validators); return $form; }