diff --git a/src/ArrayInput.php b/src/ArrayInput.php index 10219261..65777a46 100644 --- a/src/ArrayInput.php +++ b/src/ArrayInput.php @@ -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()) { diff --git a/test/ArrayInputTest.php b/test/ArrayInputTest.php index 74ac6525..96aa8e67 100644 --- a/test/ArrayInputTest.php +++ b/test/ArrayInputTest.php @@ -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);