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/5] 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/5] 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/5] 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/5] 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/5] 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); }