Question

I am new in zend, I am using zendForms and I want to group my form fields into groups then on the frontend i want to display them in different divs, also i want to do the same for my save button, is that possible ?

Was it helpful?

Solution

As usually with ZF you can do it multiple ways, the easiest one I'd suggest is to define display groups and see if default html they generate suits your needs (display groups are rendered with fieldset tag by default).

If you need more customization, see below:

class Form_Product extends Zend_Form
{
    public function init()
    {
        $a = new Zend_Form_Element_Text('a');
        $b = new Zend_Form_Element_Text('b');
        $c = new Zend_Form_Element_Text('c');

        /*
         * The first way is to define display groups and customize their decorators
         */
        $this->addDisplayGroup(array($a, $b), 'groupAB');
        $this->getDisplayGroup('groupAB')->setDisableLoadDefaultDecorators(true);
        $this->getDisplayGroup('groupAB')->setDecorators(array(
            'FormElements',
            'DtDdWrapper'
        )); // or whatever decorators you need

        $this->addDisplayGroup(array($c), 'groupC');
        // ...

        /*
         * Second way is to use custom view script to render the form. 
         * In view use $this->element to get form object 
         * and $this->element->getElements() or $this->element->getElement('name') to get elements
         */
        $this->addElements(array($a, $b, $c));

        $this->setDisableLoadDefaultDecorators(true);
        $this->setDecorators(array(
            array('ViewScript', array('viewScript' => 'controller/action/form.phtml')),
        ));
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top