Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Not set default required error message always
Browse files Browse the repository at this point in the history
Skip if a previous one was set
  • Loading branch information
Maks3w committed Sep 3, 2015
1 parent 26da9df commit 76ef192
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/ArrayInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 3 additions & 1 deletion src/FileInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 3 additions & 1 deletion src/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
13 changes: 13 additions & 0 deletions test/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 76ef192

Please sign in to comment.