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/118'
Browse files Browse the repository at this point in the history
Close #118
Fixes #114
  • Loading branch information
weierophinney committed Aug 18, 2016
2 parents 1ca10d6 + ea095ec commit 0cf1bdc
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 2.7.3 - TBD
## 2.7.3 - 2016-08-18

### Added

Expand Down Expand Up @@ -38,6 +38,9 @@ All notable changes to this project will be documented in this file, in reverse
such, `setData()` now raises `Zend\InputFilter\Exception\InvalidArgumentException`
for invalid data, ensuring that `isValid()` and `getUnknown()` only ever
operate on usable collections and collection sets.
- [#118](https://github.com/zendframework/zend-inputfilter/pull/118) fixes
aggregation of error messages when validating collections to ensure only the
error messages specific to a given datum are presented.

## 2.7.2 - 2016-06-11

Expand Down
4 changes: 4 additions & 0 deletions src/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,10 @@ public function merge(InputInterface $input)
*/
public function isValid($context = null)
{
if (is_array($this->errorMessage)) {
$this->errorMessage = null;
}

$value = $this->getValue();
$hasValue = $this->hasValue();
$empty = ($value === null || $value === '' || $value === []);
Expand Down
104 changes: 104 additions & 0 deletions test/CollectionInputFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use Zend\InputFilter\Exception\RuntimeException;
use Zend\InputFilter\Input;
use Zend\InputFilter\InputFilter;
use Zend\Validator\EmailAddress;
use Zend\Validator\NotEmpty;

/**
* @covers Zend\InputFilter\CollectionInputFilter
Expand Down Expand Up @@ -501,4 +503,106 @@ public function testSettingDataWithNonArrayNonTraversableRaisesException($data)
$this->setExpectedException(InvalidArgumentException::class, 'invalid collection');
$collectionInputFilter->setData($data);
}

public function testCollectionValidationDoesNotReuseMessagesBetweenInputs()
{
$inputFilter = new InputFilter();
$inputFilter->add([
'name' => 'email',
'required' => true,
'validators' => [
['name' => EmailAddress::class],
['name' => NotEmpty::class],
],
]);
$inputFilter->add([
'name' => 'name',
'required' => true,
'validators' => [
['name' => NotEmpty::class],
],
]);

$collectionInputFilter = $this->inputFilter;
$collectionInputFilter->setInputFilter($inputFilter);

$collectionInputFilter->setData([
[
'name' => 'Tom',
],
[
'email' => 'tom@tom',
'name' => 'Tom',
],
]);

$isValid = $collectionInputFilter->isValid();
$messages = $collectionInputFilter->getMessages();

// @codingStandardsIgnoreStart
$this->assertFalse($isValid);
$this->assertCount(2, $messages);

$this->assertArrayHasKey('email', $messages[0]);
$this->assertCount(1, $messages[0]['email']);
$this->assertContains('Value is required and can\'t be empty', $messages[0]['email']);

$this->assertArrayHasKey('email', $messages[1]);
$this->assertCount(3, $messages[1]['email']);
$this->assertNotContains('Value is required and can\'t be empty', $messages[1]['email']);
$this->assertContains('\'tom\' is not a valid hostname for the email address', $messages[1]['email']);
$this->assertContains('The input does not match the expected structure for a DNS hostname', $messages[1]['email']);
$this->assertContains('The input appears to be a local network name but local network names are not allowed', $messages[1]['email']);
// @codingStandardsIgnoreEnd
}

public function testCollectionValidationUsesCustomInputErrorMessages()
{
$inputFilter = new InputFilter();
$inputFilter->add([
'name' => 'email',
'required' => true,
'validators' => [
['name' => EmailAddress::class],
['name' => NotEmpty::class],
],
'error_message' => 'CUSTOM ERROR MESSAGE',
]);
$inputFilter->add([
'name' => 'name',
'required' => true,
'validators' => [
['name' => NotEmpty::class],
],
]);

$collectionInputFilter = $this->inputFilter;
$collectionInputFilter->setInputFilter($inputFilter);

$collectionInputFilter->setData([
[
'name' => 'Tom',
],
[
'email' => 'tom@tom',
'name' => 'Tom',
],
]);

$isValid = $collectionInputFilter->isValid();
$messages = $collectionInputFilter->getMessages();


$this->assertFalse($isValid);
$this->assertCount(2, $messages);

$this->assertArrayHasKey('email', $messages[0]);
$this->assertCount(1, $messages[0]['email']);
$this->assertContains('CUSTOM ERROR MESSAGE', $messages[0]['email']);
$this->assertNotContains('Value is required and can\'t be empty', $messages[0]['email']);

$this->assertArrayHasKey('email', $messages[1]);
$this->assertCount(1, $messages[1]['email']);
$this->assertContains('CUSTOM ERROR MESSAGE', $messages[1]['email']);
}
}

0 comments on commit 0cf1bdc

Please sign in to comment.