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

Commit eed0346

Browse files
committed
Optional input without value are valid
1 parent 16ee17f commit eed0346

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

src/FileInput.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ public function isValid($context = null)
119119
$allowEmpty = $this->allowEmpty();
120120
$continueIfEmpty = $this->continueIfEmpty();
121121

122+
if (! $hasValue && ! $required) {
123+
return true;
124+
}
125+
122126
if (! $hasValue && $required && ! $this->hasFallback()) {
123127
$this->setErrorMessage('Value is required');
124128
return false;

src/Input.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,10 @@ public function isValid($context = null)
396396
return true;
397397
}
398398

399+
if (! $hasValue && ! $required) {
400+
return true;
401+
}
402+
399403
if (! $hasValue && $required) {
400404
$this->setErrorMessage('Value is required');
401405
return false;

test/InputTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,25 @@ public function testRequiredWithoutFallbackAndValueNotSetThenFail()
175175
$this->assertEquals(['Value is required'], $input->getMessages(), 'getMessages() value not match');
176176
}
177177

178+
public function testNotRequiredWithoutFallbackAndValueNotSetThenIsValid()
179+
{
180+
$input = $this->input;
181+
$input->setRequired(false);
182+
$input->setAllowEmpty(false);
183+
$input->setContinueIfEmpty(true);
184+
185+
// Validator should not to be called
186+
$input->getValidatorChain()
187+
->attach($this->createValidatorMock(null, null))
188+
;
189+
$this->assertTrue(
190+
$input->isValid(),
191+
'isValid() should be return always true when is not required, and no data is set. Detail: ' .
192+
json_encode($input->getMessages())
193+
);
194+
$this->assertEquals([], $input->getMessages(), 'getMessages() should be empty because the input is valid');
195+
}
196+
178197
public function testNotEmptyValidatorNotInjectedIfContinueIfEmptyIsTrue()
179198
{
180199
$input = new Input('foo');

0 commit comments

Comments
 (0)