Samuele
Samuele

Reputation: 510

zend framework create empty display group and then add element

Is possible to create an empty display group and then add the elements?

I tried:

addDisplayGroup(null, 'exampleGroup')

But you have to pass at least an element...

I need to create just an empty display group.

I need to do that because after create the display group, I will add the elements one by one in a foreach....

Thanks again...

Upvotes: 1

Views: 4141

Answers (3)

Kurczakovsky
Kurczakovsky

Reputation: 75

My solution:

$zgody = $db->fetchAll($db->select()->from('rodo_regulki')->where( 'id_inwest =?', $inwest ));

$getFirstElement = $zgody[0]->id;
$this->addDisplayGroup(array('zgoda_'.$getFirstElement),'zgody', array('legend' => 'Zgody'));

foreach($zgody as $zgoda) {
    $element = 'zgoda_'.$zgoda->id;
    $addElement = $this->getElement($element);
    $displayGroup = $this->getDisplayGroup('zgody');
    $displayGroup->addElement($addElement);
}

Upvotes: 0

David Weinraub
David Weinraub

Reputation: 14184

How about overriding the form's addDisplayGroup($elements, $name) method, adding a dummy element if necessary:

class My_Form extends Zend_Form
{

    protected $dummyName = '_myDummy';

    public function addDisplayGroup(array $elements, $name)
    {
        if (count($elements) == 0){
            $elt = new Zend_Form_Element_Hidden($this->dummyName);
            $elt->setIgnore(true);
            $elements[] = $this->dummyName;
        }
        return parent::addDisplayGroup($elements, $name);
    }

    public function render(Zend_View_Interface $view = null)
    {
        $this->removeElement($this->dummyName);
        return parent::render($view);
    }
}

Then in your form you can now invoke as:

$displayGroup = $this->addDisplayGroup(array(), 'myDisplayGroup');

$elt1 = new Zend_Form_Element_Text('myElement1');
$displayGroup->addElement($elt1);

$elt2 = new Zend_Form_Element_Text('myElement2');
$displayGroup->addElement($elt2);

// etc

Upvotes: 1

dinopmi
dinopmi

Reputation: 2673

You could try with

$form->addDisplayGroup(array(), 'exampleGroup');

and then get the display group like that:

$displayGroup = $form->getDisplayGroup('exampleGroup');

and add the elements one by one later with $displayGroup->addElement() or $displayGroup->createElement().

You can see more details in the Display Groups documentation.

EDIT:

ZF doesn't allow to add a display group with no elements. The simplest solution could be to create the elements first, put them into an array, and then pass that array to addDisplayGroup.

It's also possible to instantiate a new Zend_Form_DisplayGroup object and then add it to the form using $form->addDisplayGroups(array($group)), but I doubt if it's more convenient than calling addDisplayGroup() with the elements already created.

Hope that helps,

Upvotes: 2

Related Questions