Issues

ZF-10411: Form element doesn't work after it was added with Zend_Form_DisplayGroup::addElement()

Issue Type: Bug Created: 2010-09-03T01:58:20.000+0000 Last Updated: 2010-09-09T11:46:14.000+0000 Status: Resolved Fix version(s): - 1.11.0 (02/Nov/10)

Reporter: Pavel Tytyuk (koshpavel) Assignee: Matthew Weier O'Phinney (matthew) Tags: - Zend_Form

Related issues: Attachments:

Description

Here is an example: We have form A and form B A inherited from B, B inherited from Zend_Form. Form B defines some element and adds it to display group. Form A also defines an element and adds it to display group, defined in form B with Zend_Form_DisplayGroup::addElement() method.

In this case element from form A will be rendered in form properly, but will not work as should - populate and getValue(s) doesn't work for it.

If to add element in form A to the form with Zend_Form::addElement() (not just to display group) it will rendered twice - inside groups and outside of it and became to work.

Code example

<pre class="highlight">
class Form_Bform extends App_Form
{
    public function init()
    {
        $this->addElement(
            'text',
            'el1',
            array(
                'label' => 'Title',
                'required' => true,
            )
        );


        $this->addDisplayGroup(array('el1'), 'group1', array('legend'=>'legend 1'));
    }
}




<pre class="highlight">
class Form_Aform extends Form_Bform
{
    public function init()
    {
        parent::init();
        $element = new Zend_Form_Element_Text('el2', array(
            'label' => 'Customer type',
            )
        );

        //Uncomment the following line and element will be added twice
        //$this->addElement($element);
        $this->group1->addElement($element);
    }
}

Comments

Posted by Matthew Weier O'Phinney (matthew) on 2010-09-03T06:16:52.000+0000

Element MUST be attached to a form or subform; attaching them ONLY to a DisplayGroup means they will never be rendered. In other words, you need to attach the element to the display group after it is already attached to the form.

In your Form A example, you should be adding the element to the form using $this->addElement(), and then adding it by name to the display group:

<pre class="highlight">
class Form_Aform extends Form_Bform
{
    public function init()
    {
        parent::init();
        $element = new Zend_Form_Element_Text('el2', array(
            'label' => 'Customer type',
        ));
        $this->addElement($element);

        // OR:
        $this->addElement('text', 'el2', array(
            'label' => 'Customer type',
        ));

        // Add to group:
        $this->group1->addElement($this->el2);
    }
}

Posted by Pavel Tytyuk (koshpavel) on 2010-09-03T07:04:39.000+0000

Did you miss this?

<pre class="highlight">
        //Uncomment the following line and element will be added twice
        //$this->addElement($element);
        $this->group1->addElement($element);

Your code renders "el2" field two times - inside and outside of fieldset

Posted by Matthew Weier O'Phinney (matthew) on 2010-09-03T08:04:12.000+0000

I did indeed miss that, and I've verified the issue.

Oddly, addDisplayGroup() uses the exact same methodology for adding elements as simply calling addElement(); this is going to take some debugging to properly diagnose.

Posted by Matthew Weier O'Phinney (matthew) on 2010-09-09T11:46:06.000+0000

Fixed in r22930; will release with next minor release (1.11) as it involved adding new methods.

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