Issues

ZF-5842: When cloning subforms, reset elementsBelongTo value

Description

When cloning sub forms, output can be problematic if the original sub form has already been rendered. In such a case, if the cloned sub form has been assigned a different name, the original elementsBelongTo value will persist and be used during output. As an example:


$subForm->setName('foo');
$subForm->render();

$clone = clone $subForm;
$clone->setName('bar');
echo $clone->render();

When the $clone is rendered, its items will still be subscripts of 'foo', not 'bar'.

The solution is to modify setName() slightly to detect such a situation:


    public function setName($name)
    {   
        $updateElementsBelongTo = false;
        if ($this->getElementsBelongTo() == $this->getName()) {
            $updateElementsBelongTo = true;
        }

        $name = $this->filterName($name);
        if (('0' !== $name) && empty($name)) {
            require_once 'Zend/Form/Exception.php';
            throw new Zend_Form_Exception('Invalid name provided; must contain o
        }

        $this->setAttrib('name', $name);

        if ($updateElementsBelongTo) {
            $this->setElementsBelongTo($name);
        }
        return $this;
    }

Comments

No comments to display