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

Required does not work, when array is empty #167

Merged
merged 5 commits into from
Dec 13, 2018
Merged
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
8 changes: 8 additions & 0 deletions src/ArrayInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ public function isValid($context = null)
$validator = $this->getValidatorChain();
$values = $this->getValue();
$result = true;

if ($required && empty($values)) {
if ($this->errorMessage === null) {
$this->errorMessage = $this->prepareRequiredValidationFailureMessage();
}
return false;
}

foreach ($values as $value) {
$empty = ($value === null || $value === '' || $value === []);
if ($empty && ! $this->isRequired() && ! $this->continueIfEmpty()) {
Expand Down
12 changes: 12 additions & 0 deletions test/ArrayInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ public function testDefaultGetValue()
$this->assertCount(0, $this->input->getValue());
}

public function testRequiredWithoutFallbackAndValueIsEmptyArrayThenFail()
{
$input = $this->input;
$input->setRequired(true);
$input->setValue([]);
$this->assertFalse(
$input->isValid(),
'isValid() should be return always false when no fallback value, is required, and value is empty array.'
);
$this->assertRequiredValidationErrorMessage($input);
}

public function testSetValueWithInvalidInputTypeThrowsInvalidArgumentException()
{
$this->expectException(InvalidArgumentException::class);
Expand Down