diff --git a/src/ArrayInput.php b/src/ArrayInput.php index 88f5429a..2ee27809 100644 --- a/src/ArrayInput.php +++ b/src/ArrayInput.php @@ -70,7 +70,9 @@ public function isValid($context = null) } if (! $hasValue && $required) { - $this->setErrorMessage('Value is required'); + if ($this->errorMessage === null) { + $this->setErrorMessage('Value is required'); + } return false; } diff --git a/src/FileInput.php b/src/FileInput.php index 401d54bd..48aa010e 100644 --- a/src/FileInput.php +++ b/src/FileInput.php @@ -124,7 +124,9 @@ public function isValid($context = null) } if (! $hasValue && $required && ! $this->hasFallback()) { - $this->setErrorMessage('Value is required'); + if ($this->errorMessage === null) { + $this->setErrorMessage('Value is required'); + } return false; } diff --git a/src/Input.php b/src/Input.php index d8f2b68e..50c98fc6 100644 --- a/src/Input.php +++ b/src/Input.php @@ -403,7 +403,9 @@ public function isValid($context = null) } if (! $hasValue && $required) { - $this->setErrorMessage('Value is required'); + if ($this->errorMessage === null) { + $this->setErrorMessage('Value is required'); + } return false; } diff --git a/test/InputTest.php b/test/InputTest.php index 80ddda31..bd88157c 100644 --- a/test/InputTest.php +++ b/test/InputTest.php @@ -175,6 +175,19 @@ public function testRequiredWithoutFallbackAndValueNotSetThenFail() $this->assertEquals(['Value is required'], $input->getMessages(), 'getMessages() value not match'); } + public function testRequiredWithoutFallbackAndValueNotSetThenFailWithCustomErrorMessage() + { + $input = $this->input; + $input->setRequired(true); + $input->setErrorMessage('fooErrorMessage'); + + $this->assertFalse( + $input->isValid(), + 'isValid() should be return always false when no fallback value, is required, and not data is set.' + ); + $this->assertEquals(['fooErrorMessage'], $input->getMessages(), 'getMessages() value not match'); + } + public function testNotRequiredWithoutFallbackAndValueNotSetThenIsValid() { $input = $this->input;