Skip to content
Browse files

Merge branch 'hotfix/4077' into develop

Forward port #4077
  • Loading branch information...
2 parents 06c8db1 + 58a5393 commit 3dfb349796853e97c1d5ba13008bc5d3869de61c @weierophinney weierophinney committed
View
24 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]);
+ }
+ }
+ }
}
View
44 tests/ZendTest/Form/FormTest.php
@@ -1381,4 +1381,48 @@ 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);
+ }
}
View
23 tests/ZendTest/Form/TestAsset/Entity/Category.php
@@ -13,6 +13,11 @@
class Category
{
/**
+ * @var int
+ */
+ protected $id;
+
+ /**
* @var string
*/
protected $name;
@@ -34,4 +39,22 @@ 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;
+ }
}
View
20 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);
+ }
}
View
25 tests/ZendTest/Form/TestAsset/ProductCategoriesFieldset.php
@@ -0,0 +1,25 @@
+<?php
+
+namespace ZendTest\Form\TestAsset;
+
+class ProductCategoriesFieldset extends ProductFieldset
+{
+ public function __construct()
+ {
+ parent::__construct();
+
+ $template = new CategoryFieldset();
+
+ $this->add(array(
+ 'name' => 'categories',
+ 'type' => 'collection',
+ 'options' => array(
+ 'label' => 'Categories',
+ 'should_create_template' => true,
+ 'allow_add' => true,
+ 'count' => 0,
+ 'target_element' => $template,
+ ),
+ ));
+ }
+}

0 comments on commit 3dfb349

Please sign in to comment.
Something went wrong with that request. Please try again.