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

Commit

Permalink
Merge branch 'hotfix/167' into develop
Browse files Browse the repository at this point in the history
Forward port #167

Conflicts:
	CHANGELOG.md
  • Loading branch information
weierophinney committed Dec 13, 2018
2 parents 5166f6a + 44b37b4 commit 68b5d93
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ All notable changes to this project will be documented in this file, in reverse

- Nothing.

## 2.8.3 - TBD
## 2.8.3 - 2018-12-13

### Added

Expand All @@ -44,7 +44,8 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- Nothing.
- [#167](https://github.com/zendframework/zend-inputfilter/pull/167) fixes the combination of marking an `ArrayInput` required, and passing an
empty array for validation; it now correctly detects these as invalid.

## 2.8.2 - 2018-05-14

Expand Down
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
27 changes: 27 additions & 0 deletions test/ArrayInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,33 @@ public function testDefaultGetValue()
$this->assertCount(0, $this->input->getValue());
}

public function testArrayInputMarkedRequiredWithoutAFallbackFailsValidationForEmptyArrays()
{
$input = $this->input;
$input->setRequired(true);
$input->setValue([]);

$this->assertFalse($input->isValid());
$this->assertRequiredValidationErrorMessage($input);
}

public function testArrayInputMarkedRequiredWithoutAFallbackUsesProvidedErrorMessageOnFailureDueToEmptyArray()
{
$expected = 'error message';

$input = $this->input;
$input->setRequired(true);
$input->setErrorMessage($expected);
$input->setValue([]);

$this->assertFalse($input->isValid());

$messages = $input->getMessages();
$this->assertCount(1, $messages);
$message = array_pop($messages);
$this->assertEquals($expected, $message);
}

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

0 comments on commit 68b5d93

Please sign in to comment.