Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
When binding an object to a form using \Zend\Form\Element\Collection …
Browse files Browse the repository at this point in the history
…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
feibeck committed Feb 22, 2013
1 parent 2a51b16 commit b0c03a3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
6 changes: 6 additions & 0 deletions library/Zend/Form/Element/Collection.php
Expand Up @@ -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);
}
}
}
}

Expand Down
38 changes: 38 additions & 0 deletions tests/ZendTest/Form/Element/CollectionTest.php
Expand Up @@ -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');
Expand Down

0 comments on commit b0c03a3

Please sign in to comment.