From fde58e844b8b817403f54061ed660aeae7c1621f Mon Sep 17 00:00:00 2001 From: fabiocarneiro Date: Thu, 13 Mar 2014 16:54:16 -0300 Subject: [PATCH 01/13] Collection Input Filter fix messages --- src/CollectionInputFilter.php | 37 +++++++++-------------------------- 1 file changed, 9 insertions(+), 28 deletions(-) diff --git a/src/CollectionInputFilter.php b/src/CollectionInputFilter.php index ce1884f5..28eacea6 100644 --- a/src/CollectionInputFilter.php +++ b/src/CollectionInputFilter.php @@ -80,7 +80,6 @@ public function setInputFilter($inputFilter) } $this->inputFilter = $inputFilter; - $this->inputs = $inputFilter->getInputs(); return $this; } @@ -181,37 +180,19 @@ public function isValid() if (!is_array($data)) { $data = array(); } - $this->data = $data; - $this->populate(); - - if ($this->validateInputs($inputs, $data)) { - $this->collectionValidInputs[$key] = $this->validInputs; + $this->inputFilter->setData($data); + + if ($this->inputFilter->isValid()) { + $this->collectionValidInputs[$key] = $this->inputFilter->validInputs; } else { - $this->collectionInvalidInputs[$key] = $this->invalidInputs; $valid = false; + $this->collectionMessages[$key] = $this->inputFilter->getMessages(); + $this->collectionInvalidInputs[$key] = $this->inputFilter->invalidInputs; } - $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; - } - } - $this->collectionValues[$key] = $values; - $this->collectionRawValues[$key] = $rawValues; - + $this->collectionValues[$key] = $this->inputFilter->getValues(); + $this->collectionRawValues[$key] = $this->inputFilter->getRawValues(); + if (!empty($messages)) { $this->collectionMessages[$key] = $messages; } From 8964b4f87f694945ae3d074730a18f698496f868 Mon Sep 17 00:00:00 2001 From: fabiocarneiro Date: Thu, 13 Mar 2014 18:04:25 -0300 Subject: [PATCH 02/13] Remove trailing spaces --- src/CollectionInputFilter.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/CollectionInputFilter.php b/src/CollectionInputFilter.php index 28eacea6..e170195c 100644 --- a/src/CollectionInputFilter.php +++ b/src/CollectionInputFilter.php @@ -181,7 +181,7 @@ public function isValid() $data = array(); } $this->inputFilter->setData($data); - + if ($this->inputFilter->isValid()) { $this->collectionValidInputs[$key] = $this->inputFilter->validInputs; } else { @@ -190,9 +190,9 @@ public function isValid() $this->collectionInvalidInputs[$key] = $this->inputFilter->invalidInputs; } - $this->collectionValues[$key] = $this->inputFilter->getValues(); + $this->collectionValues[$key] = $this->inputFilter->getValues(); $this->collectionRawValues[$key] = $this->inputFilter->getRawValues(); - + if (!empty($messages)) { $this->collectionMessages[$key] = $messages; } From 8052d7eff44fd061b57b368d7e8dba06b0454523 Mon Sep 17 00:00:00 2001 From: fabiocarneiro Date: Thu, 13 Mar 2014 19:02:15 -0300 Subject: [PATCH 03/13] Fix for weird test trying to validate a CollectionInputFilter with a Input instead of a InputFilter --- test/InputFilterTest.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/InputFilterTest.php b/test/InputFilterTest.php index ae727d9e..003d6032 100644 --- a/test/InputFilterTest.php +++ b/test/InputFilterTest.php @@ -51,9 +51,13 @@ public function testCanAddUsingSpecification() */ 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( From 42f46b70dcbfa47bd8024aea1a9f1d665977e23f Mon Sep 17 00:00:00 2001 From: fabiocarneiro Date: Thu, 13 Mar 2014 19:49:07 -0300 Subject: [PATCH 04/13] Remove unecessary test, and fix for validation groups on collections --- test/CollectionInputFilterTest.php | 11 ----------- 1 file changed, 11 deletions(-) mode change 100644 => 100755 test/CollectionInputFilterTest.php diff --git a/test/CollectionInputFilterTest.php b/test/CollectionInputFilterTest.php old mode 100644 new mode 100755 index 50c3226f..77e029ac --- 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()); From f21d45194f9b2e3127b77c5787cca738d262331d Mon Sep 17 00:00:00 2001 From: fabiocarneiro Date: Thu, 13 Mar 2014 19:51:58 -0300 Subject: [PATCH 05/13] fix for validation groups on collections --- src/CollectionInputFilter.php | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) mode change 100644 => 100755 src/CollectionInputFilter.php diff --git a/src/CollectionInputFilter.php b/src/CollectionInputFilter.php old mode 100644 new mode 100755 index e170195c..bf0e69df --- a/src/CollectionInputFilter.php +++ b/src/CollectionInputFilter.php @@ -204,22 +204,18 @@ public function isValid() /** * {@inheritdoc} */ - public function setValidationGroup($name) + public function setValidationGroup($groups) { - if ($name === self::VALIDATE_ALL) { + if ($groups === 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); + if (is_array($groups)) { + foreach($groups as $group) { + $this->inputFilter->setValidationGroup($group); + } } - - return parent::setValidationGroup(func_get_args()); } /** From c707928111fbebb57ab4f1f27a130ce56527065d Mon Sep 17 00:00:00 2001 From: fabiocarneiro Date: Thu, 13 Mar 2014 20:17:17 -0300 Subject: [PATCH 06/13] Old behavior failure test case (nested collections) --- test/CollectionInputFilterTest.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/test/CollectionInputFilterTest.php b/test/CollectionInputFilterTest.php index 77e029ac..ed5f082e 100755 --- a/test/CollectionInputFilterTest.php +++ b/test/CollectionInputFilterTest.php @@ -327,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()); @@ -334,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() From 8f62405fa30e5d6537b04d0218af6ab9847ba9d4 Mon Sep 17 00:00:00 2001 From: fabiocarneiro Date: Fri, 14 Mar 2014 10:22:04 -0300 Subject: [PATCH 07/13] Remove dead code --- src/CollectionInputFilter.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/CollectionInputFilter.php b/src/CollectionInputFilter.php index bf0e69df..12b08e4a 100755 --- a/src/CollectionInputFilter.php +++ b/src/CollectionInputFilter.php @@ -193,9 +193,6 @@ public function isValid() $this->collectionValues[$key] = $this->inputFilter->getValues(); $this->collectionRawValues[$key] = $this->inputFilter->getRawValues(); - if (!empty($messages)) { - $this->collectionMessages[$key] = $messages; - } } return $valid; From ecdbd4de2d9226f0cd21c47e582e75f347edfab7 Mon Sep 17 00:00:00 2001 From: fabiocarneiro Date: Mon, 17 Mar 2014 11:55:48 -0300 Subject: [PATCH 08/13] Fix for direct property access --- src/CollectionInputFilter.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/CollectionInputFilter.php b/src/CollectionInputFilter.php index 12b08e4a..f524b70e 100755 --- a/src/CollectionInputFilter.php +++ b/src/CollectionInputFilter.php @@ -175,7 +175,7 @@ public function isValid() return $valid; } - $inputs = $this->validationGroup ?: array_keys($this->inputs); + $inputs = $this->validationGroup ?: array_keys($this->inputFilter->getInputs()); foreach ($this->collectionData as $key => $data) { if (!is_array($data)) { $data = array(); @@ -183,11 +183,11 @@ public function isValid() $this->inputFilter->setData($data); if ($this->inputFilter->isValid()) { - $this->collectionValidInputs[$key] = $this->inputFilter->validInputs; + $this->collectionValidInputs[$key] = $this->inputFilter->getValidInput(); } else { $valid = false; $this->collectionMessages[$key] = $this->inputFilter->getMessages(); - $this->collectionInvalidInputs[$key] = $this->inputFilter->invalidInputs; + $this->collectionInvalidInputs[$key] = $this->inputFilter->getInvalidInput(); } $this->collectionValues[$key] = $this->inputFilter->getValues(); From 142f18c1a8239c4f4615e1f5e6640f6e33dd1cab Mon Sep 17 00:00:00 2001 From: fabiocarneiro Date: Mon, 17 Mar 2014 12:44:16 -0300 Subject: [PATCH 09/13] Remove trailing spaces --- src/CollectionInputFilter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CollectionInputFilter.php b/src/CollectionInputFilter.php index f524b70e..1d747def 100755 --- a/src/CollectionInputFilter.php +++ b/src/CollectionInputFilter.php @@ -211,7 +211,7 @@ public function setValidationGroup($groups) if (is_array($groups)) { foreach($groups as $group) { $this->inputFilter->setValidationGroup($group); - } + } } } From f9667b1cf3f5ec1a8976521fcefbe1f62f57a31f Mon Sep 17 00:00:00 2001 From: fabiocarneiro Date: Mon, 17 Mar 2014 13:38:56 -0300 Subject: [PATCH 10/13] Revert names and use parent variables --- src/CollectionInputFilter.php | 41 ++++++++++-------------------- test/CollectionInputFilterTest.php | 2 +- test/InputFilterTest.php | 4 +-- 3 files changed, 16 insertions(+), 31 deletions(-) mode change 100755 => 100644 src/CollectionInputFilter.php mode change 100755 => 100644 test/CollectionInputFilterTest.php diff --git a/src/CollectionInputFilter.php b/src/CollectionInputFilter.php old mode 100755 new mode 100644 index 1d747def..6ea736e4 --- a/src/CollectionInputFilter.php +++ b/src/CollectionInputFilter.php @@ -13,20 +13,6 @@ class CollectionInputFilter extends InputFilter { - /* - * @var array - */ - protected $collectionData; - - /* - * @var array - */ - protected $collectionValidInputs; - - /* - * @var array - */ - protected $collectionInvalidInputs; /* * @var bool @@ -139,7 +125,7 @@ public function setCount($count) public function getCount() { if (null === $this->count) { - $this->count = count($this->collectionData); + $this->count = count($this->data); } return $this->count; } @@ -149,7 +135,7 @@ public function getCount() */ public function setData($data) { - $this->collectionData = $data; + $this->data = $data; } /** @@ -165,29 +151,28 @@ 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->inputFilter->getInputs()); - foreach ($this->collectionData as $key => $data) { + foreach ($this->data as $key => $data) { if (!is_array($data)) { $data = array(); } $this->inputFilter->setData($data); if ($this->inputFilter->isValid()) { - $this->collectionValidInputs[$key] = $this->inputFilter->getValidInput(); + $this->validInputs[$key] = $this->inputFilter->getValidInput(); } else { $valid = false; $this->collectionMessages[$key] = $this->inputFilter->getMessages(); - $this->collectionInvalidInputs[$key] = $this->inputFilter->getInvalidInput(); + $this->invalidInputs[$key] = $this->inputFilter->getInvalidInput(); } $this->collectionValues[$key] = $this->inputFilter->getValues(); @@ -201,15 +186,15 @@ public function isValid() /** * {@inheritdoc} */ - public function setValidationGroup($groups) + public function setValidationGroup($name) { - if ($groups === self::VALIDATE_ALL) { + if ($name === self::VALIDATE_ALL) { $this->validationGroup = null; return $this; } - if (is_array($groups)) { - foreach($groups as $group) { + if (is_array($name)) { + foreach($name as $group) { $this->inputFilter->setValidationGroup($group); } } @@ -220,7 +205,7 @@ public function setValidationGroup($groups) */ public function getInvalidInput() { - return (is_array($this->collectionInvalidInputs) ? $this->collectionInvalidInputs : array()); + return (is_array($this->invalidInputs) ? $this->invalidInputs : array()); } /** @@ -228,7 +213,7 @@ public function getInvalidInput() */ public function getValidInput() { - return (is_array($this->collectionValidInputs) ? $this->collectionValidInputs : array()); + return (is_array($this->validInputs) ? $this->validInputs : array()); } /** diff --git a/test/CollectionInputFilterTest.php b/test/CollectionInputFilterTest.php old mode 100755 new mode 100644 index ed5f082e..b1b9f3d4 --- a/test/CollectionInputFilterTest.php +++ b/test/CollectionInputFilterTest.php @@ -350,7 +350,7 @@ public function testGetMessagesForInvalidInputs() } $messages = $this->filter->getMessages(); - + $this->assertCount(3, $messages); $this->assertArrayHasKey('foo', $messages[0]); $this->assertArrayHasKey('bar', $messages[1]); diff --git a/test/InputFilterTest.php b/test/InputFilterTest.php index 003d6032..478f18ef 100644 --- a/test/InputFilterTest.php +++ b/test/InputFilterTest.php @@ -53,11 +53,11 @@ public function testCountZeroValidateInternalInputWithCollectionInputFilter() { $inputFilter = new InputFilter(); $inputFilter->add(new Input(), 'name'); - + $collection = new CollectionInputFilter(); $collection->setInputFilter($inputFilter); $collection->setCount(0); - + $this->filter->add($collection, 'people'); $data = array( From e77949655eebe608b36379796e943626f1189421 Mon Sep 17 00:00:00 2001 From: fabiocarneiro Date: Mon, 17 Mar 2014 17:57:46 -0300 Subject: [PATCH 11/13] Remove duplicated code --- src/CollectionInputFilter.php | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/CollectionInputFilter.php b/src/CollectionInputFilter.php index 6ea736e4..d190ee68 100644 --- a/src/CollectionInputFilter.php +++ b/src/CollectionInputFilter.php @@ -200,22 +200,6 @@ public function setValidationGroup($name) } } - /** - * {@inheritdoc} - */ - public function getInvalidInput() - { - return (is_array($this->invalidInputs) ? $this->invalidInputs : array()); - } - - /** - * {@inheritdoc} - */ - public function getValidInput() - { - return (is_array($this->validInputs) ? $this->validInputs : array()); - } - /** * {@inheritdoc} */ From 18f91855326ab7d7babaa6142fca6e60c0ab7596 Mon Sep 17 00:00:00 2001 From: fabiocarneiro Date: Tue, 18 Mar 2014 11:34:24 -0300 Subject: [PATCH 12/13] Direct property access and validation groups --- src/CollectionInputFilter.php | 38 +++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/src/CollectionInputFilter.php b/src/CollectionInputFilter.php index d190ee68..1ed4876c 100644 --- a/src/CollectionInputFilter.php +++ b/src/CollectionInputFilter.php @@ -1,4 +1,5 @@ inputFilter = $inputFilter; + return $this; } @@ -79,6 +80,7 @@ public function getInputFilter() if (null === $this->inputFilter) { $this->setInputFilter(new InputFilter()); } + return $this->inputFilter; } @@ -91,6 +93,7 @@ public function getInputFilter() public function setIsRequired($isRequired) { $this->isRequired = $isRequired; + return $this; } @@ -104,7 +107,6 @@ public function getIsRequired() return $this->isRequired; } - /** * Set the count of data to validate * @@ -114,6 +116,7 @@ public function getIsRequired() public function setCount($count) { $this->count = $count > 0 ? $count : 0; + return $this; } @@ -127,6 +130,7 @@ public function getCount() if (null === $this->count) { $this->count = count($this->data); } + return $this->count; } @@ -158,6 +162,7 @@ public function isValid() if (empty($this->data)) { $this->clearValues(); $this->clearRawValues(); + return $valid; } @@ -165,19 +170,22 @@ public function isValid() if (!is_array($data)) { $data = array(); } - $this->inputFilter->setData($data); + $this->getInputFilter()->setData($data); - if ($this->inputFilter->isValid()) { - $this->validInputs[$key] = $this->inputFilter->getValidInput(); + if (null !== $this->validationGroup) { + $this->getInputFilter()->setValidationGroup($this->validationGroup[$key]); + } + + if ($this->getInputFilter()->isValid()) { + $this->validInputs[$key] = $this->getInputFilter()->getValidInput(); } else { $valid = false; - $this->collectionMessages[$key] = $this->inputFilter->getMessages(); - $this->invalidInputs[$key] = $this->inputFilter->getInvalidInput(); + $this->collectionMessages[$key] = $this->getInputFilter()->getMessages(); + $this->invalidInputs[$key] = $this->getInputFilter()->getInvalidInput(); } - $this->collectionValues[$key] = $this->inputFilter->getValues(); - $this->collectionRawValues[$key] = $this->inputFilter->getRawValues(); - + $this->collectionValues[$key] = $this->getInputFilter()->getValues(); + $this->collectionRawValues[$key] = $this->getInputFilter()->getRawValues(); } return $valid; @@ -190,14 +198,13 @@ public function setValidationGroup($name) { if ($name === self::VALIDATE_ALL) { $this->validationGroup = null; + return $this; } - if (is_array($name)) { - foreach($name as $group) { - $this->inputFilter->setValidationGroup($group); - } - } + $this->validationGroup = $name; + + return $this; } /** @@ -243,4 +250,5 @@ public function getMessages() { return $this->collectionMessages; } + } From d00e4aad5317bd0bad7ba68a99c81a08b4c5acff Mon Sep 17 00:00:00 2001 From: fabiocarneiro Date: Tue, 18 Mar 2014 14:26:23 -0300 Subject: [PATCH 13/13] Multiple function call fix --- src/CollectionInputFilter.php | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/CollectionInputFilter.php b/src/CollectionInputFilter.php index 1ed4876c..6e8261af 100644 --- a/src/CollectionInputFilter.php +++ b/src/CollectionInputFilter.php @@ -1,5 +1,4 @@ getInputFilter(); $valid = true; if ($this->getCount() < 1) { @@ -170,22 +170,22 @@ public function isValid() if (!is_array($data)) { $data = array(); } - $this->getInputFilter()->setData($data); + $inputFilter->setData($data); if (null !== $this->validationGroup) { - $this->getInputFilter()->setValidationGroup($this->validationGroup[$key]); + $inputFilter->setValidationGroup($this->validationGroup[$key]); } - if ($this->getInputFilter()->isValid()) { - $this->validInputs[$key] = $this->getInputFilter()->getValidInput(); + if ($inputFilter->isValid()) { + $this->validInputs[$key] = $inputFilter->getValidInput(); } else { $valid = false; - $this->collectionMessages[$key] = $this->getInputFilter()->getMessages(); - $this->invalidInputs[$key] = $this->getInputFilter()->getInvalidInput(); + $this->collectionMessages[$key] = $inputFilter->getMessages(); + $this->invalidInputs[$key] = $inputFilter->getInvalidInput(); } - $this->collectionValues[$key] = $this->getInputFilter()->getValues(); - $this->collectionRawValues[$key] = $this->getInputFilter()->getRawValues(); + $this->collectionValues[$key] = $inputFilter->getValues(); + $this->collectionRawValues[$key] = $inputFilter->getRawValues(); } return $valid; @@ -250,5 +250,4 @@ public function getMessages() { return $this->collectionMessages; } - }