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

Commit

Permalink
Merge branch 'hotfix/4077' into develop
Browse files Browse the repository at this point in the history
Forward port #4077
  • Loading branch information
weierophinney committed Mar 28, 2013
2 parents 06c8db1 + 58a5393 commit 3dfb349
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 0 deletions.
24 changes: 24 additions & 0 deletions library/Zend/Form/Element/Collection.php
Expand Up @@ -233,6 +233,8 @@ public function populateValues($data)
)
);
}

$this->replaceTemplateObjects();
}

/**
Expand Down Expand Up @@ -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]);
}
}
}
}
44 changes: 44 additions & 0 deletions tests/ZendTest/Form/FormTest.php
Expand Up @@ -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);
}
}
23 changes: 23 additions & 0 deletions tests/ZendTest/Form/TestAsset/Entity/Category.php
Expand Up @@ -12,6 +12,11 @@

class Category
{
/**
* @var int
*/
protected $id;

/**
* @var string
*/
Expand All @@ -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;
}
}
20 changes: 20 additions & 0 deletions tests/ZendTest/Form/TestAsset/Entity/Product.php
Expand Up @@ -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);
}
}
25 changes: 25 additions & 0 deletions 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.