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

Commit 96dcadd

Browse files
committed
Merge branch 'hotfix/51'
Close #51 Fixes #50
2 parents b36d648 + 352603e commit 96dcadd

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
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
@@ -398,6 +398,10 @@ public function isValid($context = null)
398398
return true;
399399
}
400400

401+
if (! $hasValue && ! $required) {
402+
return true;
403+
}
404+
401405
if (! $hasValue && $required) {
402406
$this->setErrorMessage('Value is required');
403407
return false;

test/InputTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,31 @@ public function testRequiredWithoutFallbackAndValueNotSetThenFail()
167167
{
168168
$input = $this->input;
169169
$input->setRequired(true);
170-
$input->setContinueIfEmpty(true);
171170

172171
$this->assertFalse(
173172
$input->isValid(),
174173
'isValid() should be return always false when no fallback value, is required, and not data is set.'
175174
);
176-
$this->assertEquals(['Value is required'], $input->getMessages(), 'getMessages() should be empty because the input is valid');
175+
$this->assertEquals(['Value is required'], $input->getMessages(), 'getMessages() value not match');
176+
}
177+
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');
177195
}
178196

179197
public function testNotEmptyValidatorNotInjectedIfContinueIfEmptyIsTrue()

0 commit comments

Comments
 (0)