From 5a0859e4f520f66710713fa05b12d99f24b5da2a Mon Sep 17 00:00:00 2001 From: eweso <6918714+eweso@users.noreply.github.com> Date: Thu, 17 May 2018 12:30:22 +0200 Subject: [PATCH 1/7] Required does not work, when array is empty The first problem is, that hasValue always returns true, when a value is set, even, if the value is an empty array. So the first validation for the required filter will be skipped. The second problem is, that the isRequired is called within the foreach loop. But if the value is an empty array, the loop will never start, so isRequired will never triggered and the method will return the defaultValue true. --- src/ArrayInput.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/ArrayInput.php b/src/ArrayInput.php index 10219261..448627c4 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()) { From 968b33b9ad65164d68cae0e21a64e293a0d0097e Mon Sep 17 00:00:00 2001 From: eweso <6918714+eweso@users.noreply.github.com> Date: Mon, 4 Jun 2018 16:51:59 +0200 Subject: [PATCH 2/7] Removed whitespace on line 86 and 93 To pass test "continuous-integration/travis-ci/pr" --- src/ArrayInput.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ArrayInput.php b/src/ArrayInput.php index 448627c4..65777a46 100644 --- a/src/ArrayInput.php +++ b/src/ArrayInput.php @@ -83,14 +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()) { From b0717ce53a73e92c8c4c4f170ef9e6c38dddd534 Mon Sep 17 00:00:00 2001 From: eweso <6918714+eweso@users.noreply.github.com> Date: Thu, 12 Jul 2018 16:58:59 +0200 Subject: [PATCH 3/7] Update ArrayInputTest.php --- test/ArrayInputTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/ArrayInputTest.php b/test/ArrayInputTest.php index 74ac6525..4b6a7952 100644 --- a/test/ArrayInputTest.php +++ b/test/ArrayInputTest.php @@ -26,6 +26,19 @@ public function testDefaultGetValue() { $this->assertCount(0, $this->input->getValue()); } + + public function testRequiredWithoutFallbackAndValueIsEmptyArrayThenFail() + { + $input = $this->input; + $input->setRequired(true); + $input->setValue([]); + + $this->assertFalse( + $input->isValid(), + 'isValid() should be return always false when no fallback value, is required, and not data is set.' + ); + $this->assertRequiredValidationErrorMessage($input); + } public function testSetValueWithInvalidInputTypeThrowsInvalidArgumentException() { From 47fc580bd1c1f48319943980c030d0919bbaa339 Mon Sep 17 00:00:00 2001 From: eweso <6918714+eweso@users.noreply.github.com> Date: Thu, 12 Jul 2018 17:13:12 +0200 Subject: [PATCH 4/7] Update ArrayInputTest.php --- test/ArrayInputTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/ArrayInputTest.php b/test/ArrayInputTest.php index 4b6a7952..bdf71023 100644 --- a/test/ArrayInputTest.php +++ b/test/ArrayInputTest.php @@ -26,7 +26,7 @@ public function testDefaultGetValue() { $this->assertCount(0, $this->input->getValue()); } - + public function testRequiredWithoutFallbackAndValueIsEmptyArrayThenFail() { $input = $this->input; From 0812223722da1b82affe53d480fb4b3c2d04f5c5 Mon Sep 17 00:00:00 2001 From: eweso <6918714+eweso@users.noreply.github.com> Date: Thu, 12 Jul 2018 17:17:58 +0200 Subject: [PATCH 5/7] Update ArrayInputTest.php --- test/ArrayInputTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/ArrayInputTest.php b/test/ArrayInputTest.php index bdf71023..96aa8e67 100644 --- a/test/ArrayInputTest.php +++ b/test/ArrayInputTest.php @@ -32,10 +32,9 @@ public function testRequiredWithoutFallbackAndValueIsEmptyArrayThenFail() $input = $this->input; $input->setRequired(true); $input->setValue([]); - $this->assertFalse( $input->isValid(), - 'isValid() should be return always false when no fallback value, is required, and not data is set.' + 'isValid() should be return always false when no fallback value, is required, and value is empty array.' ); $this->assertRequiredValidationErrorMessage($input); } From 2bf3c6ece98395f453bf33bb60fd75384f2a4aff Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Thu, 13 Dec 2018 16:49:46 -0600 Subject: [PATCH 6/7] Test improvements for #167 - Renames `testRequiredWithoutFallbackAndValueIsEmptyArrayThenFail` to `testArrayInputMarkedRequiredWithoutAFallbackFailsValidationForEmptyArrays`. - Adds `testArrayInputMarkedRequiredWithoutAFallbackUsesProvidedErrorMessageOnFailureDueToEmptyArray` to validate that if an error message is set on the input, that value is used instead of the defaults. --- test/ArrayInputTest.php | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/test/ArrayInputTest.php b/test/ArrayInputTest.php index 96aa8e67..5ce72d95 100644 --- a/test/ArrayInputTest.php +++ b/test/ArrayInputTest.php @@ -27,18 +27,33 @@ public function testDefaultGetValue() $this->assertCount(0, $this->input->getValue()); } - public function testRequiredWithoutFallbackAndValueIsEmptyArrayThenFail() + public function testArrayInputMarkedRequiredWithoutAFallbackFailsValidationForEmptyArrays() { $input = $this->input; $input->setRequired(true); $input->setValue([]); - $this->assertFalse( - $input->isValid(), - 'isValid() should be return always false when no fallback value, is required, and value is empty array.' - ); + + $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); From 44b37b483bc9b5b1ee29cdf315f22ff841515fb6 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Thu, 13 Dec 2018 16:51:44 -0600 Subject: [PATCH 7/7] Adds CHANGELOG entry for #167 --- CHANGELOG.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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