Issues

ZF-12117: Clone Zend_Form will not update ID's

Issue Type: Bug Created: 2012-03-22T16:28:40.000+0000 Last Updated: 2012-03-28T20:20:34.000+0000 Status: Open Fix version(s): Reporter: Chuck Reeves (creeves1982) Assignee: Matthew Weier O'Phinney (matthew) Tags: - Zend_Form

Related issues: Attachments:

Description

When Cloning Zend_Form, the Id will not be updated thus causing ID Collisions.

Example:

<pre class="highlight">
$test = new Zend_Form();
$test->addElement('text', 'foo');

echo $test;

$new = clone $test;
echo $new;

Produces:

<pre class="highlight">


 


 

Recommend work around is to have a counter that will increment when the __clone method is called. Zend_Form should then append that number to the id:

<pre class="highlight">
class Zend_Form
{
    protected static $_cloneCount = 0;

    // ..snip ..
        public function __clone()
    {
        self::$_cloneCount++;
        $elements = array();
        foreach ($this->getElements() as $name => $element) {
            $element = clone $element;
            $newId  = $element->getId() . '-' . self::$_cloneCount;
            $element->setAttrib('id', $newId);

            $elements[] = $element;
        }
        $this->setElements($elements);
    
        //duplicate below for display groups and subforms
    }

}

Comments

Posted by Chuck Reeves (creeves1982) on 2012-03-22T16:36:14.000+0000

Sorry copied the clone wrong

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts