From b0c03a331565960599de338311cc56cb940c4655 Mon Sep 17 00:00:00 2001 From: Florian Eibeck Date: Fri, 15 Feb 2013 14:10:47 +0100 Subject: [PATCH] When binding an object to a form using \Zend\Form\Element\Collection the collection of objects for the fieldsets should not be created new after form validation. During data extraction, all fieldsets in the collection are given their respective object. --- library/Zend/Form/Element/Collection.php | 6 +++ .../ZendTest/Form/Element/CollectionTest.php | 38 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/library/Zend/Form/Element/Collection.php b/library/Zend/Form/Element/Collection.php index 18652325e87..12306208aed 100644 --- a/library/Zend/Form/Element/Collection.php +++ b/library/Zend/Form/Element/Collection.php @@ -477,6 +477,12 @@ public function extract() $targetElement = clone $this->targetElement; $targetElement->object = $value; $values[$key] = $targetElement->extract(); + if ($this->has($key)) { + $fieldset = $this->get($key); + if ($fieldset instanceof Fieldset && $fieldset->allowObjectBinding($value)) { + $fieldset->setObject($value); + } + } } } diff --git a/tests/ZendTest/Form/Element/CollectionTest.php b/tests/ZendTest/Form/Element/CollectionTest.php index 12f0c540f49..6fec4349fa7 100644 --- a/tests/ZendTest/Form/Element/CollectionTest.php +++ b/tests/ZendTest/Form/Element/CollectionTest.php @@ -284,6 +284,44 @@ public function testExtractFromObjectDoesntTouchOriginalObject() $this->assertSame($originalObjectHash,$objectAfterExtractHash); } + public function testDoesNotCreateNewObjects() + { + $form = new \Zend\Form\Form(); + $form->setHydrator(new \Zend\Stdlib\Hydrator\ClassMethods()); + $this->productFieldset->setUseAsBaseFieldset(true); + $form->add($this->productFieldset); + + $product = new Product(); + $product->setName("foo"); + $product->setPrice(42); + $cat1 = new \ZendTest\Form\TestAsset\Entity\Category(); + $cat1->setName("bar"); + $cat2 = new \ZendTest\Form\TestAsset\Entity\Category(); + $cat2->setName("bar2"); + + $product->setCategories(array($cat1,$cat2)); + + $form->bind($product); + + $form->setData( + array("product"=> + array( + "name" => "franz", + "price" => 13, + "categories" => array( + array("name" => "sepp"), + array("name" => "herbert") + ) + ) + ) + ); + $form->isValid(); + + $categories = $product->getCategories(); + $this->assertSame($categories[0], $cat1); + $this->assertSame($categories[1], $cat2); + } + public function testExtractDefaultIsEmptyArray() { $collection = $this->form->get('fieldsets');