Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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.
  • Loading branch information...
commit b0c03a331565960599de338311cc56cb940c4655 1 parent 2a51b16
@feibeck feibeck authored
View
6 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);
+ }
+ }
}
}
View
38 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');
Please sign in to comment.
Something went wrong with that request. Please try again.