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

Commit a597a80

Browse files
committed
Merge branch 'hotfix/118' into develop
Forward port #118 Conflicts: CHANGELOG.md
2 parents 0fd87e8 + ea095ec commit a597a80

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ All notable changes to this project will be documented in this file, in reverse
2020

2121
- Nothing.
2222

23-
## 2.7.3 - TBD
23+
## 2.7.3 - 2016-08-18
2424

2525
### Added
2626

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

6063
## 2.7.2 - 2016-06-11
6164

src/Input.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,10 @@ public function merge(InputInterface $input)
387387
*/
388388
public function isValid($context = null)
389389
{
390+
if (is_array($this->errorMessage)) {
391+
$this->errorMessage = null;
392+
}
393+
390394
$value = $this->getValue();
391395
$hasValue = $this->hasValue();
392396
$empty = ($value === null || $value === '' || $value === []);

test/CollectionInputFilterTest.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
use Zend\InputFilter\Exception\RuntimeException;
2020
use Zend\InputFilter\Input;
2121
use Zend\InputFilter\InputFilter;
22+
use Zend\Validator\EmailAddress;
23+
use Zend\Validator\NotEmpty;
2224

2325
/**
2426
* @covers Zend\InputFilter\CollectionInputFilter
@@ -501,4 +503,106 @@ public function testSettingDataWithNonArrayNonTraversableRaisesException($data)
501503
$this->setExpectedException(InvalidArgumentException::class, 'invalid collection');
502504
$collectionInputFilter->setData($data);
503505
}
506+
507+
public function testCollectionValidationDoesNotReuseMessagesBetweenInputs()
508+
{
509+
$inputFilter = new InputFilter();
510+
$inputFilter->add([
511+
'name' => 'email',
512+
'required' => true,
513+
'validators' => [
514+
['name' => EmailAddress::class],
515+
['name' => NotEmpty::class],
516+
],
517+
]);
518+
$inputFilter->add([
519+
'name' => 'name',
520+
'required' => true,
521+
'validators' => [
522+
['name' => NotEmpty::class],
523+
],
524+
]);
525+
526+
$collectionInputFilter = $this->inputFilter;
527+
$collectionInputFilter->setInputFilter($inputFilter);
528+
529+
$collectionInputFilter->setData([
530+
[
531+
'name' => 'Tom',
532+
],
533+
[
534+
'email' => 'tom@tom',
535+
'name' => 'Tom',
536+
],
537+
]);
538+
539+
$isValid = $collectionInputFilter->isValid();
540+
$messages = $collectionInputFilter->getMessages();
541+
542+
// @codingStandardsIgnoreStart
543+
$this->assertFalse($isValid);
544+
$this->assertCount(2, $messages);
545+
546+
$this->assertArrayHasKey('email', $messages[0]);
547+
$this->assertCount(1, $messages[0]['email']);
548+
$this->assertContains('Value is required and can\'t be empty', $messages[0]['email']);
549+
550+
$this->assertArrayHasKey('email', $messages[1]);
551+
$this->assertCount(3, $messages[1]['email']);
552+
$this->assertNotContains('Value is required and can\'t be empty', $messages[1]['email']);
553+
$this->assertContains('\'tom\' is not a valid hostname for the email address', $messages[1]['email']);
554+
$this->assertContains('The input does not match the expected structure for a DNS hostname', $messages[1]['email']);
555+
$this->assertContains('The input appears to be a local network name but local network names are not allowed', $messages[1]['email']);
556+
// @codingStandardsIgnoreEnd
557+
}
558+
559+
public function testCollectionValidationUsesCustomInputErrorMessages()
560+
{
561+
$inputFilter = new InputFilter();
562+
$inputFilter->add([
563+
'name' => 'email',
564+
'required' => true,
565+
'validators' => [
566+
['name' => EmailAddress::class],
567+
['name' => NotEmpty::class],
568+
],
569+
'error_message' => 'CUSTOM ERROR MESSAGE',
570+
]);
571+
$inputFilter->add([
572+
'name' => 'name',
573+
'required' => true,
574+
'validators' => [
575+
['name' => NotEmpty::class],
576+
],
577+
]);
578+
579+
$collectionInputFilter = $this->inputFilter;
580+
$collectionInputFilter->setInputFilter($inputFilter);
581+
582+
$collectionInputFilter->setData([
583+
[
584+
'name' => 'Tom',
585+
],
586+
[
587+
'email' => 'tom@tom',
588+
'name' => 'Tom',
589+
],
590+
]);
591+
592+
$isValid = $collectionInputFilter->isValid();
593+
$messages = $collectionInputFilter->getMessages();
594+
595+
596+
$this->assertFalse($isValid);
597+
$this->assertCount(2, $messages);
598+
599+
$this->assertArrayHasKey('email', $messages[0]);
600+
$this->assertCount(1, $messages[0]['email']);
601+
$this->assertContains('CUSTOM ERROR MESSAGE', $messages[0]['email']);
602+
$this->assertNotContains('Value is required and can\'t be empty', $messages[0]['email']);
603+
604+
$this->assertArrayHasKey('email', $messages[1]);
605+
$this->assertCount(1, $messages[1]['email']);
606+
$this->assertContains('CUSTOM ERROR MESSAGE', $messages[1]['email']);
607+
}
504608
}

0 commit comments

Comments
 (0)