Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fixes an issue when binding a model to a form collection element #3873

Closed
wants to merge 1 commit into from

2 participants

@feibeck

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.

@feibeck feibeck 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.
b0c03a3
@weierophinney weierophinney was assigned
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/3873'
Close #3873
d60df97
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/3873' into develop
Forward port #3873
5c52baa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 22, 2013
  1. @feibeck

    When binding an object to a form using \Zend\Form\Element\Collection …

    feibeck authored
    …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.
This page is out of date. Refresh to see the latest.
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');
Something went wrong with that request. Please try again.