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

Not set default required error message always #60

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't what #28 was getting at.

Prior to 2.4, a required input that was provided an empty value would return [ 'isEmpty' => "Value is required and can't be empty"] for the messages; that's what the reporter wants to happen again.

I'll work on this using your patch as a start, and submit an alternate PR.


$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