ZF-10731: addDisplayGroup() should automatically call addElement()

Issue Type: Improvement Created: 2010-11-23T07:31:38.000+0000 Last Updated: 2011-01-01T09:54:54.000+0000 Status: Resolved Fix version(s): - 1.11.3 (01/Feb/11)

Reporter: Martin Keckeis (thadafinser) Assignee: Richard Tuin (richardtuin) Tags: - Zend_Form

Related issues: - ZF-10491

Attachments: - uploads.php


When always using fieldsets with labels, it's very annoying to always use this procedure:

$lenDateFrom = new Zend_Form_Element_Text('lenDateFrom'); $lenDateFrom->setValue($dateFrom); $lenDateFrom->setRequired(); $this->addElement($lenDateFrom);

...more Elements....

$this->addDisplayGroup(array('lenDateFrom', '....', '...', ..), 'name', array('legend' => 'mainData') );

Because that is to much work, I created an wrapper, which would maybe be fine in Zend_Form to:

How does this wrapper works? See example in attached file.

What does the wrapper do? call $this->addElement() call/create addDisplayGroup

How is the call now? $lenDateFrom = new Zend_Form_Element_Text('lenDateFrom'); $lenDateFrom->setValue($dateFrom); $lenDateFrom->setRequired(); $this->addFieldset($lenDateFrom, 'mainData');

At the end of the Form renderFieldsets() is called.

It would need some modifications to be more abstract, but maybe the code snippet is usefull.


Posted by Martin Keckeis (thadafinser) on 2010-11-23T07:35:32.000+0000


Posted by Kai Uwe (kaiuwe) on 2010-11-23T09:39:02.000+0000

@Martin Keckeis Please use code tags! Thanks.

Here's is my suggestion:

<pre class="highlight">
public function addDisplayGroup(array $elements, $name, $options = null)
    $group = array();
    foreach ($elements as $element) {
        $elementName = null;
        if (is_string($element)) {
            $elementName = $element;
        if ($element instanceof Zend_Form_Element) {
            $elementName = $element->getName();
            if (!isset($this->_elements[$elementName])) {
        if (isset($this->_elements[$elementName])) {
            $add = $this->getElement($elementName);
            if (null !== $add) {
                $group[] = $add;

Posted by Richard Tuin (richardtuin) on 2010-11-23T13:39:20.000+0000

This issue is related to ZF-10491.

Posted by Richard Tuin (richardtuin) on 2010-11-27T05:53:00.000+0000

The attached patch automatically adds the Zend_Form_Element's in $element to the form. Therefore calling Zend_Form::addElement won't be mandatory anymore.

Also, unit tests are included, as well as my fix to ZF-10734 (which isn't committed at this moment)

Posted by Ramon Henrique Ornelas (ramon) on 2011-01-01T09:54:54.000+0000

Fixed r23603 in trunk merged to release branch 1.11 r23604 - thanks.

Have you found an issue?

See the Overview section for more details.


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

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