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

Commit

Permalink
Merge pull request zendframework/zendframework#5820 from demichl68/ma…
Browse files Browse the repository at this point in the history
…ster

[Zend\InputFilter\InputFilter] SetValidationGroup() VALIDATE_ALL not working recursively
  • Loading branch information
weierophinney committed Mar 4, 2014
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/BaseInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,11 @@ public function setValidationGroup($name)
{
if ($name === self::VALIDATE_ALL) {
$this->validationGroup = null;
foreach($this->getInputs() as $input) {
if($input instanceof InputFilterInterface) {
$input->setValidationGroup(self::VALIDATE_ALL);
}
}
return $this;
}

Expand All @@ -386,6 +391,14 @@ public function setValidationGroup($name)
} else {
$inputs[] = $key;

if (!$this->inputs[$key] instanceof InputFilterInterface) {
throw new Exception\InvalidArgumentException(
sprintf(
'Input "%s" must implement InputFilterInterface',
$key
)
);
}
// Recursively populate validation groups for sub input filters
$this->inputs[$key]->setValidationGroup($value);
}
Expand Down
35 changes: 35 additions & 0 deletions test/BaseInputFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,41 @@ public function testCanValidatePartialDataset()
$this->assertFalse($filter->isValid());
}

public function testResetEmptyValidationGroupRecursively()
{
$data = array(
'flat' => 'foo',
'deep' => array(
'deep-input1' => 'deep-foo1',
'deep-input2' => 'deep-foo2',
)
);
$filter = new InputFilter;
$filter->add(new Input, 'flat');
$deepInputFilter = new InputFilter;
$deepInputFilter->add(new Input, 'deep-input1');
$deepInputFilter->add(new Input, 'deep-input2');
$filter->add($deepInputFilter, 'deep');
$filter->setData($data);
$filter->setValidationGroup(array('deep' => 'deep-input1'));
// reset validation group
$filter->setValidationGroup(InputFilter::VALIDATE_ALL);
$this->assertEquals($data, $filter->getValues());
}

public function testSetDeepValidationGroupToNonInputFilterThrowsException()
{
$filter = $this->getInputFilter();
$filter->add(new Input, 'flat');
// we expect setValidationGroup to throw an exception when flat is treated
// like an inputfilter which it actually isn't
$this->setExpectedException(
'Zend\InputFilter\Exception\InvalidArgumentException',
'Input "flat" must implement InputFilterInterface'
);
$filter->setValidationGroup(array('flat' => 'foo'));
}

public function testCanRetrieveInvalidInputsOnFailedValidation()
{
if (!extension_loaded('intl')) {
Expand Down

0 comments on commit 1470cb6

Please sign in to comment.