From 7febaca06c19bbab27d36dfc0a2309a273000c68 Mon Sep 17 00:00:00 2001 From: Samu Juvonen Date: Tue, 19 Mar 2013 21:07:42 +0200 Subject: [PATCH 1/4] Fix bug with not preserving the original entity objects in a Collection upon validating the form --- library/Zend/Form/Element/Collection.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/library/Zend/Form/Element/Collection.php b/library/Zend/Form/Element/Collection.php index 12306208aed..c525292d983 100644 --- a/library/Zend/Form/Element/Collection.php +++ b/library/Zend/Form/Element/Collection.php @@ -233,6 +233,8 @@ public function populateValues($data) ) ); } + + $this->replaceTemplateObjects(); } /** @@ -536,4 +538,26 @@ protected function createTemplateElement() return $elementOrFieldset; } + + /** + * Replaces the default template object of a sub element with the corresponding + * real entity so that all properties are preserved. + * + * @return void + */ + protected function replaceTemplateObjects() + { + $fieldsets = $this->getFieldsets(); + + if (!count($fieldsets) || !$this->object) { + return; + } + + foreach ($fieldsets as $fieldset) { + $i = $fieldset->getName(); + if (isset($this->object[$i])) { + $fieldset->setObject($this->object[$i]); + } + } + } } From 7f56498f60c684458b2d00f100ac0ca262748ee9 Mon Sep 17 00:00:00 2001 From: Samu Juvonen Date: Tue, 19 Mar 2013 21:08:54 +0200 Subject: [PATCH 2/4] Add test case for verifying that a Collection preserves original entity objects upon form validation --- tests/ZendTest/Form/FormTest.php | 43 +++++++++++++++++++ .../Form/TestAsset/Entity/Category.php | 22 ++++++++++ .../Form/TestAsset/Entity/Product.php | 20 +++++++++ 3 files changed, 85 insertions(+) diff --git a/tests/ZendTest/Form/FormTest.php b/tests/ZendTest/Form/FormTest.php index 2c0d20f0187..326b6e3452e 100644 --- a/tests/ZendTest/Form/FormTest.php +++ b/tests/ZendTest/Form/FormTest.php @@ -1357,4 +1357,47 @@ public function testGetValidationGroupReturnsNullWhenNoneSet() { $this->assertNull($this->form->getValidationGroup()); } + + public function testPreserveEntitiesBoundToCollectionAfterValidation() { + $this->form->setInputFilter(new \Zend\InputFilter\InputFilter()); + $fieldset = new TestAsset\ProductCategoriesFieldset(); + $fieldset->setUseAsBaseFieldset(true); + + $product = new Entity\Product(); + $product->setName('Foobar'); + $product->setPrice(100); + + $c1 = new Entity\Category(); + $c1->setId(1); + $c1->setName('First Category'); + + $c2 = new Entity\Category(); + $c2->setId(2); + $c2->setName('Second Category'); + + $product->setCategories(array($c1, $c2)); + + $this->form->add($fieldset); + $this->form->bind($product); + + $data = array( + 'product' => array( + 'name' => 'Barbar', + 'price' => 200, + 'categories' => array( + array('name' => 'Something else'), + array('name' => 'Totally different'), + ), + ), + ); + + $hash1 = spl_object_hash($this->form->getObject()->getCategory(0)); + $this->form->setData($data); + $this->form->isValid(); + $hash2 = spl_object_hash($this->form->getObject()->getCategory(0)); + + // Returned object has to be the same as when binding or properties + // will be lost. (For example entity IDs.) + $this->assertTrue($hash1 == $hash2); + } } diff --git a/tests/ZendTest/Form/TestAsset/Entity/Category.php b/tests/ZendTest/Form/TestAsset/Entity/Category.php index 581a3ed3ef9..f388b973182 100644 --- a/tests/ZendTest/Form/TestAsset/Entity/Category.php +++ b/tests/ZendTest/Form/TestAsset/Entity/Category.php @@ -12,6 +12,11 @@ class Category { + /** + * @var int + */ + protected $id; + /** * @var string */ @@ -34,4 +39,21 @@ public function getName() { return $this->name; } + + /** + * @param int $id + * @return Product + */ + public function setId($id) + { + $this->id = $id; + return $this; + } + + /** + * @return int + */ + public function getId() { + return $this->id; + } } diff --git a/tests/ZendTest/Form/TestAsset/Entity/Product.php b/tests/ZendTest/Form/TestAsset/Entity/Product.php index 0efa23cdb6a..4b2ee5cf63b 100644 --- a/tests/ZendTest/Form/TestAsset/Entity/Product.php +++ b/tests/ZendTest/Form/TestAsset/Entity/Product.php @@ -80,4 +80,24 @@ public function getPrice() { return $this->price; } + + /** + * Return category from index + * + * @param int $i + */ + public function getCategory($i) + { + return $this->categories[$i]; + } + + /** + * Required when binding to a form + * + * @return array + */ + public function getArrayCopy() + { + return get_object_vars($this); + } } From 70464e32372ae6cce1d63f0d89b6fbcf651a31f5 Mon Sep 17 00:00:00 2001 From: Samu Juvonen Date: Tue, 19 Mar 2013 21:09:39 +0200 Subject: [PATCH 3/4] Add fieldset class needed by Zend\Form\FormTest::testPreserveEntitiesBoundToCollectionAfterValidation --- .../TestAsset/ProductCategoriesFieldset.php | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 tests/ZendTest/Form/TestAsset/ProductCategoriesFieldset.php diff --git a/tests/ZendTest/Form/TestAsset/ProductCategoriesFieldset.php b/tests/ZendTest/Form/TestAsset/ProductCategoriesFieldset.php new file mode 100644 index 00000000000..1d45d3082d7 --- /dev/null +++ b/tests/ZendTest/Form/TestAsset/ProductCategoriesFieldset.php @@ -0,0 +1,24 @@ +add(array( + 'name' => 'categories', + 'type' => 'collection', + 'options' => array( + 'label' => 'Categories', + 'should_create_template' => true, + 'allow_add' => true, + 'count' => 0, + 'target_element' => $template, + ), + )); + } +} From 58a5393526eadbe65d0d40c573b26fe4f89593c6 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Thu, 28 Mar 2013 11:30:19 -0500 Subject: [PATCH 4/4] [#4077] CS fixes - braces --- tests/ZendTest/Form/FormTest.php | 3 ++- tests/ZendTest/Form/TestAsset/Entity/Category.php | 3 ++- tests/ZendTest/Form/TestAsset/ProductCategoriesFieldset.php | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/ZendTest/Form/FormTest.php b/tests/ZendTest/Form/FormTest.php index 326b6e3452e..6e00d04b2d9 100644 --- a/tests/ZendTest/Form/FormTest.php +++ b/tests/ZendTest/Form/FormTest.php @@ -1358,7 +1358,8 @@ public function testGetValidationGroupReturnsNullWhenNoneSet() $this->assertNull($this->form->getValidationGroup()); } - public function testPreserveEntitiesBoundToCollectionAfterValidation() { + public function testPreserveEntitiesBoundToCollectionAfterValidation() + { $this->form->setInputFilter(new \Zend\InputFilter\InputFilter()); $fieldset = new TestAsset\ProductCategoriesFieldset(); $fieldset->setUseAsBaseFieldset(true); diff --git a/tests/ZendTest/Form/TestAsset/Entity/Category.php b/tests/ZendTest/Form/TestAsset/Entity/Category.php index f388b973182..4a5f45431df 100644 --- a/tests/ZendTest/Form/TestAsset/Entity/Category.php +++ b/tests/ZendTest/Form/TestAsset/Entity/Category.php @@ -53,7 +53,8 @@ public function setId($id) /** * @return int */ - public function getId() { + public function getId() + { return $this->id; } } diff --git a/tests/ZendTest/Form/TestAsset/ProductCategoriesFieldset.php b/tests/ZendTest/Form/TestAsset/ProductCategoriesFieldset.php index 1d45d3082d7..df72829a2a8 100644 --- a/tests/ZendTest/Form/TestAsset/ProductCategoriesFieldset.php +++ b/tests/ZendTest/Form/TestAsset/ProductCategoriesFieldset.php @@ -4,7 +4,8 @@ class ProductCategoriesFieldset extends ProductFieldset { - public function __construct() { + public function __construct() + { parent::__construct(); $template = new CategoryFieldset();