From 094a037ca9d3322551484dd707e68120577772a6 Mon Sep 17 00:00:00 2001 From: marhub Date: Mon, 6 Mar 2017 19:09:25 +0100 Subject: [PATCH 1/3] Regression fix Fix to allow setting multiple validation fields in form of $name = ['field1','field2'] when those fields are simple Zend\InputFilter\Input --- src/BaseInputFilter.php | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/BaseInputFilter.php b/src/BaseInputFilter.php index 76765276..c971bed0 100644 --- a/src/BaseInputFilter.php +++ b/src/BaseInputFilter.php @@ -309,17 +309,12 @@ public function setValidationGroup($name) $inputs[] = $key; - if (! $this->inputs[$key] instanceof InputFilterInterface) { - throw new Exception\InvalidArgumentException( - sprintf( - 'Input "%s" must implement InputFilterInterface', - $key - ) - ); + if ($this->inputs[$key] instanceof InputFilterInterface) { + // Recursively populate validation groups for sub input filters + $this->inputs[$key]->setValidationGroup($value); } - // Recursively populate validation groups for sub input filters - $this->inputs[$key]->setValidationGroup($value); + } } else { $inputs = func_get_args(); From 0bde4d04bea33fd0e0bd6b4af1369a0099c05bcc Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Tue, 7 Nov 2017 11:04:13 -0600 Subject: [PATCH 2/3] Adds regression tests for #131 Renames the test `testSetValidationGroupThrowExceptionIfInputIsNotAnInputFilter()` to `testSetValidationGroupSkipsRecursionWhenInputIsNotAnInputFilter()`, and updates the expectations to indicate that the validation group is successfully created. It also adds a test to demonstrate expected recursion. --- src/BaseInputFilter.php | 2 -- test/BaseInputFilterTest.php | 27 ++++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/BaseInputFilter.php b/src/BaseInputFilter.php index c971bed0..f3c52156 100644 --- a/src/BaseInputFilter.php +++ b/src/BaseInputFilter.php @@ -313,8 +313,6 @@ public function setValidationGroup($name) // Recursively populate validation groups for sub input filters $this->inputs[$key]->setValidationGroup($value); } - - } } else { $inputs = func_get_args(); diff --git a/test/BaseInputFilterTest.php b/test/BaseInputFilterTest.php index 985f439a..1685020b 100644 --- a/test/BaseInputFilterTest.php +++ b/test/BaseInputFilterTest.php @@ -125,7 +125,7 @@ public function testIsValidThrowExceptionIfDataWasNotSetYet() $inputFilter->isValid(); } - public function testSetValidationGroupThrowExceptionIfInputIsNotAnInputFilter() + public function testSetValidationGroupSkipsRecursionWhenInputIsNotAnInputFilter() { $inputFilter = $this->inputFilter; @@ -133,9 +133,30 @@ public function testSetValidationGroupThrowExceptionIfInputIsNotAnInputFilter() $nestedInput = $this->createMock(InputInterface::class); $inputFilter->add($nestedInput, 'fooInput'); - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Input "fooInput" must implement InputFilterInterface'); $inputFilter->setValidationGroup(['fooInput' => 'foo']); + $this->assertAttributeEquals(['fooInput'], 'validationGroup', $inputFilter); + } + + public function testSetValidationGroupAllowsSpecifyingArrayOfInputsToNestedInputFilter() + { + $inputFilter = $this->inputFilter; + + $nestedInputFilter = new BaseInputFilter(); + + /** @var InputInterface|MockObject $nestedInput1 */ + $nestedInput1 = $this->createMock(InputInterface::class); + $nestedInputFilter->add($nestedInput1, 'nested-input1'); + + /** @var InputInterface|MockObject $nestedInput2 */ + $nestedInput2 = $this->createMock(InputInterface::class); + $nestedInputFilter->add($nestedInput2, 'nested-input2'); + + $inputFilter->add($nestedInputFilter, 'nested'); + + $inputFilter->setValidationGroup(['nested' => ['nested-input1', 'nested-input2']]); + + $this->assertAttributeEquals(['nested'], 'validationGroup', $inputFilter); + $this->assertAttributeEquals(['nested-input1', 'nested-input2'], 'validationGroup', $nestedInputFilter); } public function testSetValidationGroupThrowExceptionIfInputFilterNotExists() From d3818da6174bd1eb04a9cde3d6cbb705b0f59be3 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Tue, 7 Nov 2017 11:07:27 -0600 Subject: [PATCH 3/3] Adds CHANGELOG for #131 --- CHANGELOG.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 04fdd235..96dc2cde 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ All notable changes to this project will be documented in this file, in reverse chronological order by release. -## 2.7.5 - TBD +## 2.7.5 - 2017-11-07 ### Added @@ -30,6 +30,14 @@ All notable changes to this project will be documented in this file, in reverse previously, as soon as one item in the collection failed, the same validation message was propagated to all other items. This is now resolved. +- [#131](https://github.com/zendframework/zend-inputfilter/pull/131) fixes a + regression introduced in version 2.2.6 within + `BaseInputFilter::setValidatorGroup()` whereby it began emitting exceptions if + a given input was not an input filter. This raises issues when mixing input + filters and inputs in the same validator group specification, as you will + generally provide the input names as keys instead of values. The patch provide + ensures both styles work going forwards. + ## 2.7.4 - 2017-05-18 ### Added