ZF-5842: When cloning subforms, reset elementsBelongTo value


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:


$clone = clone $subForm;
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) {
        return $this;


No comments to display