From 16ee17f7b4a54932acea4d4581c931eccb32a3a7 Mon Sep 17 00:00:00 2001 From: Maks3w Date: Wed, 2 Sep 2015 22:10:46 +0200 Subject: [PATCH 1/2] Improve test --- test/InputTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/InputTest.php b/test/InputTest.php index 2ac49c53..ad7d42c9 100644 --- a/test/InputTest.php +++ b/test/InputTest.php @@ -167,13 +167,12 @@ public function testRequiredWithoutFallbackAndValueNotSetThenFail() { $input = $this->input; $input->setRequired(true); - $input->setContinueIfEmpty(true); $this->assertFalse( $input->isValid(), 'isValid() should be return always false when no fallback value, is required, and not data is set.' ); - $this->assertEquals(['Value is required'], $input->getMessages(), 'getMessages() should be empty because the input is valid'); + $this->assertEquals(['Value is required'], $input->getMessages(), 'getMessages() value not match'); } public function testNotEmptyValidatorNotInjectedIfContinueIfEmptyIsTrue() From eed0346c9543895eb8af1979505950af093616b8 Mon Sep 17 00:00:00 2001 From: Maks3w Date: Wed, 2 Sep 2015 22:19:46 +0200 Subject: [PATCH 2/2] Optional input without value are valid --- src/FileInput.php | 4 ++++ src/Input.php | 4 ++++ test/InputTest.php | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/src/FileInput.php b/src/FileInput.php index b10395ba..899b01cb 100644 --- a/src/FileInput.php +++ b/src/FileInput.php @@ -119,6 +119,10 @@ public function isValid($context = null) $allowEmpty = $this->allowEmpty(); $continueIfEmpty = $this->continueIfEmpty(); + if (! $hasValue && ! $required) { + return true; + } + if (! $hasValue && $required && ! $this->hasFallback()) { $this->setErrorMessage('Value is required'); return false; diff --git a/src/Input.php b/src/Input.php index 2eda11ee..6a8d9303 100644 --- a/src/Input.php +++ b/src/Input.php @@ -396,6 +396,10 @@ public function isValid($context = null) return true; } + if (! $hasValue && ! $required) { + return true; + } + if (! $hasValue && $required) { $this->setErrorMessage('Value is required'); return false; diff --git a/test/InputTest.php b/test/InputTest.php index ad7d42c9..ed0c981f 100644 --- a/test/InputTest.php +++ b/test/InputTest.php @@ -175,6 +175,25 @@ public function testRequiredWithoutFallbackAndValueNotSetThenFail() $this->assertEquals(['Value is required'], $input->getMessages(), 'getMessages() value not match'); } + public function testNotRequiredWithoutFallbackAndValueNotSetThenIsValid() + { + $input = $this->input; + $input->setRequired(false); + $input->setAllowEmpty(false); + $input->setContinueIfEmpty(true); + + // Validator should not to be called + $input->getValidatorChain() + ->attach($this->createValidatorMock(null, null)) + ; + $this->assertTrue( + $input->isValid(), + 'isValid() should be return always true when is not required, and no data is set. Detail: ' . + json_encode($input->getMessages()) + ); + $this->assertEquals([], $input->getMessages(), 'getMessages() should be empty because the input is valid'); + } + public function testNotEmptyValidatorNotInjectedIfContinueIfEmptyIsTrue() { $input = new Input('foo');