diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c0cb318..d93950f2 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.8.3 - TBD +## 2.8.3 - 2018-12-13 ### Added @@ -22,7 +22,8 @@ All notable changes to this project will be documented in this file, in reverse ### Fixed -- Nothing. +- [#167](https://github.com/zendframework/zend-inputfilter/pull/167) fixes the combination of marking an `ArrayInput` required, and passing an + empty array for validation; it now correctly detects these as invalid. ## 2.8.2 - 2018-05-14 diff --git a/src/ArrayInput.php b/src/ArrayInput.php index 10219261..65777a46 100644 --- a/src/ArrayInput.php +++ b/src/ArrayInput.php @@ -83,6 +83,14 @@ public function isValid($context = null) $validator = $this->getValidatorChain(); $values = $this->getValue(); $result = true; + + if ($required && empty($values)) { + if ($this->errorMessage === null) { + $this->errorMessage = $this->prepareRequiredValidationFailureMessage(); + } + return false; + } + foreach ($values as $value) { $empty = ($value === null || $value === '' || $value === []); if ($empty && ! $this->isRequired() && ! $this->continueIfEmpty()) { diff --git a/test/ArrayInputTest.php b/test/ArrayInputTest.php index 74ac6525..5ce72d95 100644 --- a/test/ArrayInputTest.php +++ b/test/ArrayInputTest.php @@ -27,6 +27,33 @@ public function testDefaultGetValue() $this->assertCount(0, $this->input->getValue()); } + public function testArrayInputMarkedRequiredWithoutAFallbackFailsValidationForEmptyArrays() + { + $input = $this->input; + $input->setRequired(true); + $input->setValue([]); + + $this->assertFalse($input->isValid()); + $this->assertRequiredValidationErrorMessage($input); + } + + public function testArrayInputMarkedRequiredWithoutAFallbackUsesProvidedErrorMessageOnFailureDueToEmptyArray() + { + $expected = 'error message'; + + $input = $this->input; + $input->setRequired(true); + $input->setErrorMessage($expected); + $input->setValue([]); + + $this->assertFalse($input->isValid()); + + $messages = $input->getMessages(); + $this->assertCount(1, $messages); + $message = array_pop($messages); + $this->assertEquals($expected, $message); + } + public function testSetValueWithInvalidInputTypeThrowsInvalidArgumentException() { $this->expectException(InvalidArgumentException::class);