diff --git a/src/CollectionInputFilter.php b/src/CollectionInputFilter.php index ce1884f5..6e8261af 100644 --- a/src/CollectionInputFilter.php +++ b/src/CollectionInputFilter.php @@ -13,21 +13,6 @@ class CollectionInputFilter extends InputFilter { - /* - * @var array - */ - protected $collectionData; - - /* - * @var array - */ - protected $collectionValidInputs; - - /* - * @var array - */ - protected $collectionInvalidInputs; - /* * @var bool */ @@ -80,7 +65,7 @@ public function setInputFilter($inputFilter) } $this->inputFilter = $inputFilter; - $this->inputs = $inputFilter->getInputs(); + return $this; } @@ -94,6 +79,7 @@ public function getInputFilter() if (null === $this->inputFilter) { $this->setInputFilter(new InputFilter()); } + return $this->inputFilter; } @@ -106,6 +92,7 @@ public function getInputFilter() public function setIsRequired($isRequired) { $this->isRequired = $isRequired; + return $this; } @@ -119,7 +106,6 @@ public function getIsRequired() return $this->isRequired; } - /** * Set the count of data to validate * @@ -129,6 +115,7 @@ public function getIsRequired() public function setCount($count) { $this->count = $count > 0 ? $count : 0; + return $this; } @@ -140,8 +127,9 @@ public function setCount($count) public function getCount() { if (null === $this->count) { - $this->count = count($this->collectionData); + $this->count = count($this->data); } + return $this->count; } @@ -150,7 +138,7 @@ public function getCount() */ public function setData($data) { - $this->collectionData = $data; + $this->data = $data; } /** @@ -158,6 +146,7 @@ public function setData($data) */ public function isValid() { + $inputFilter = $this->getInputFilter(); $valid = true; if ($this->getCount() < 1) { @@ -166,55 +155,37 @@ public function isValid() } } - if (count($this->collectionData) < $this->getCount()) { + if (count($this->data) < $this->getCount()) { $valid = false; } - if (empty($this->collectionData)) { + if (empty($this->data)) { $this->clearValues(); $this->clearRawValues(); + return $valid; } - $inputs = $this->validationGroup ?: array_keys($this->inputs); - foreach ($this->collectionData as $key => $data) { + foreach ($this->data as $key => $data) { if (!is_array($data)) { $data = array(); } - $this->data = $data; - $this->populate(); + $inputFilter->setData($data); - if ($this->validateInputs($inputs, $data)) { - $this->collectionValidInputs[$key] = $this->validInputs; - } else { - $this->collectionInvalidInputs[$key] = $this->invalidInputs; - $valid = false; + if (null !== $this->validationGroup) { + $inputFilter->setValidationGroup($this->validationGroup[$key]); } - $values = array(); - $rawValues = array(); - $messages = array(); - foreach ($inputs as $name) { - $input = $this->inputs[$name]; - - if ($input instanceof InputFilterInterface) { - $values[$name] = $input->getValues(); - $rawValues[$name] = $input->getRawValues(); - continue; - } - $values[$name] = $input->getValue($this->data); - $rawValues[$name] = $input->getRawValue(); - $tmpMessages = $input->getMessages(); - if (!empty($tmpMessages)) { - $messages[$name] = $tmpMessages; - } + if ($inputFilter->isValid()) { + $this->validInputs[$key] = $inputFilter->getValidInput(); + } else { + $valid = false; + $this->collectionMessages[$key] = $inputFilter->getMessages(); + $this->invalidInputs[$key] = $inputFilter->getInvalidInput(); } - $this->collectionValues[$key] = $values; - $this->collectionRawValues[$key] = $rawValues; - if (!empty($messages)) { - $this->collectionMessages[$key] = $messages; - } + $this->collectionValues[$key] = $inputFilter->getValues(); + $this->collectionRawValues[$key] = $inputFilter->getRawValues(); } return $valid; @@ -227,34 +198,13 @@ public function setValidationGroup($name) { if ($name === self::VALIDATE_ALL) { $this->validationGroup = null; - return $this; - } - if (is_array($name)) { - // Best effort check if the validation group was set by a form for BC - if (count($name) == count($this->collectionData) && is_array(reset($name))) { - return parent::setValidationGroup(reset($name)); - } - return parent::setValidationGroup($name); + return $this; } - return parent::setValidationGroup(func_get_args()); - } - - /** - * {@inheritdoc} - */ - public function getInvalidInput() - { - return (is_array($this->collectionInvalidInputs) ? $this->collectionInvalidInputs : array()); - } + $this->validationGroup = $name; - /** - * {@inheritdoc} - */ - public function getValidInput() - { - return (is_array($this->collectionValidInputs) ? $this->collectionValidInputs : array()); + return $this; } /** diff --git a/test/CollectionInputFilterTest.php b/test/CollectionInputFilterTest.php index 50c3226f..b1b9f3d4 100644 --- a/test/CollectionInputFilterTest.php +++ b/test/CollectionInputFilterTest.php @@ -109,17 +109,6 @@ public function testSetInputFilter() $this->assertInstanceOf('Zend\InputFilter\BaseInputFilter', $this->filter->getInputFilter()); } - public function testInputFilterInputsAppliedToCollection() - { - if (!extension_loaded('intl')) { - $this->markTestSkipped('ext/intl not enabled'); - } - - $this->filter->setInputFilter($this->getBaseInputFilter()); - - $this->assertCount(4, $this->filter->getInputs()); - } - public function testGetDefaultInputFilter() { $this->assertInstanceOf('Zend\InputFilter\BaseInputFilter', $this->filter->getInputFilter()); @@ -338,6 +327,16 @@ public function testGetMessagesForInvalidInputs() 'baz' => '', ), ), + array( + 'foo' => ' bazbat ', + 'bar' => '12345', + 'baz' => '', + 'nest' => array( + // missing 'foo' here + 'bar' => '12345', + 'baz' => '', + ), + ), ); $this->filter->setInputFilter($this->getBaseInputFilter()); @@ -345,19 +344,21 @@ public function testGetMessagesForInvalidInputs() $this->assertFalse($this->filter->isValid()); - $this->assertCount(2, $this->filter->getInvalidInput()); + $this->assertCount(3, $this->filter->getInvalidInput()); foreach ($this->filter->getInvalidInput() as $invalidInputs) { $this->assertCount(1, $invalidInputs); } $messages = $this->filter->getMessages(); - $this->assertCount(2, $messages); + $this->assertCount(3, $messages); $this->assertArrayHasKey('foo', $messages[0]); $this->assertArrayHasKey('bar', $messages[1]); + $this->assertArrayHasKey('nest', $messages[2]); $this->assertCount(1, $messages[0]['foo']); $this->assertCount(1, $messages[1]['bar']); + $this->assertCount(1, $messages[2]['nest']); } public function testSetValidationGroupUsingFormStyle() diff --git a/test/InputFilterTest.php b/test/InputFilterTest.php index 8ddb85e6..af999142 100644 --- a/test/InputFilterTest.php +++ b/test/InputFilterTest.php @@ -75,9 +75,13 @@ public function testGetValueReturnsArrayIfNestedInputFilters() */ public function testCountZeroValidateInternalInputWithCollectionInputFilter() { + $inputFilter = new InputFilter(); + $inputFilter->add(new Input(), 'name'); + $collection = new CollectionInputFilter(); - $collection->setCount(0) - ->add(new Input(), 'name'); + $collection->setInputFilter($inputFilter); + $collection->setCount(0); + $this->filter->add($collection, 'people'); $data = array(