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 diff --git a/src/BaseInputFilter.php b/src/BaseInputFilter.php index 76765276..f3c52156 100644 --- a/src/BaseInputFilter.php +++ b/src/BaseInputFilter.php @@ -309,17 +309,10 @@ 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(); 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()