Tab Container Enabled Forms

Nathan Gar­ling­ton wrote a nice solu­tion for Tab based forms; Yes, it is pos­si­ble to dis­play a form in a tab­Con­tainer. I do it all the time, includ­ing using other dijit con­tain­ers as well. keep in mind that this is just my solution…there are prob­a­bly other ways to do this. Feel free to cus­tomize it accord­ing to your needs. Also, you may notice that I don’t include an action attrib or method attrib dec­la­ra­tion to the form node itself…this is because I han­dle form sub­mit­ting via xhr. Exam­ple code below:


class My_Form extends Zend_Dojo_Form
{
public function init()
{
$this->setDisableLoadDefaultDecorators(true);

// setup the our default dec­o­ra­tors
$this->setDecorators(array(
’FormEle­ments’,
array(‘TabContainer’, array(
‘id’ => ‘myTab­Con­tainer’,
‘style’ => ‘width: 950px; height: 420px;’,
‘dijit­Params’ => array(‘tabPosition’ => ‘top’),
)),
’Dijit­Form’,
));

$this->setName(‘myAddTrailerForm’);

$this->setElementDecorators(array(
array(‘DijitElement’),
array(‘Description’),
array(‘HtmlTag’, array(‘tag’ => ‘dd’)),
array(‘Label’, array(‘tag’ => ‘dt’)),
));

//call cus­tom class meth­ods to add form mem­bers
$this->_addElements()
->_addDisplayGroups();

}

pri­vate func­tion _addElements()
{
$this->addElements(array(

// gen­eral infor­ma­tion dis­play group ele­ments
new Zend_Dojo_Form_Element_FilteringSelect(‘field1’, array(
’label’ => ‘First Field:’,
’mul­ti­Op­tions’ =>(array(
’option1’ => ‘option1’,
’option2’ => ‘option2’
)),
’required’ => true,
)),

new Zend_Dojo_Form_Element_CheckBox(‘cb1’, array(
’label’ => ‘Are you sure?:’,
)),

new Zend_Dojo_Form_Element_FilteringSelect(‘year’, array(
’label’ => ‘Year:’,
’mul­ti­Op­tions’ => array(/*.…*/),
’required’ => true,
)),

new Zend_Dojo_Form_Element_ComboBox(‘comboBox1’, array(
’label’ => ‘ComboBox1:’,
’mul­ti­Op­tions’ => array(/*.…*/),
’required’ => true,
’auto­com­plete’ => false,
’attribs’ => (array(‘propercase’ => true, ‘trim’ =>true)),
)),

new Zend_Dojo_Form_Element_ValidationTextBox(‘validationBox1’, array(
’label’ => ‘ValidationBox1:’,
’required’ => true,
’attribs’ => array(‘uppercase’ => true, ‘trim’ =>true),
)),

new Zend_Dojo_Form_Element_ValidationTextBox(‘validationBox2’, array(
’label’ => ‘ValidationBox2:’,
’required’ => true,
’invalidMes­sage’ => “Please enter a value”,
’attribs’ => array(‘maxlength’ => 17, ‘upper­case’ => true, ‘trim’ =>true),
’fil­ters’ => array(‘StringToUpper’),
’val­ida­tors’ => array(‘Alnum’),
)),
)); // end $this->addElements

return $this;
} // end _addElements()

/**
* Cre­ate the tabbed con­tainer lay­out
*/
pri­vate func­tion _addDisplayGroups()
{

// add the dis­play groups
$this->addDisplayGroup(
array( // ele­ments in the dis­play­Group
’field1’,
‘cb1’
),
’gen­er­al­In­for­ma­tion’ // dis­play­Group­Name
);

$this->generalInformation->setDecorators(array(
’FormEle­ments’,
array(‘HtmlTag’, array(‘tag’ => ‘dl’)),
array(
’Con­tent­Pane’, array(
’title’ => ‘Gen­eral Infor­ma­tion’
)
)
));

$this->addDisplayGroup(
array(
’year’,
‘comboBox1’,
),
’tab2’
);

$this->tab2->setDecorators(array(
‘FormEle­ments’,
array(‘HtmlTag’, array(‘tag’ => ‘dl’),
array(
‘Con­tent­Pane’, array(
‘title’ => ‘Tab 2′
)
)
));

$this->addDisplayGroup(
array(
’validationBox1’,
’validationBox2’,
),
’tab2’
);

$this->tab2->setDecorators(array(
‘FormEle­ments’,
array(‘HtmlTag’, array(‘tag’ => ‘dl’, ‘class’ => ‘addTrailer’)),
array(
‘Con­tent­Pane’, array(
‘title’ => ‘Axles’
)
)
));

return $this;
} // end _addDisplayGroups();

pub­lic func­tion but­ton­sSub­Form()
{
$sub­Form = new Zend_Dojo_Form_SubForm();
$subForm->setDecorators(array(
’FormEle­ments’,
array(‘HtmlTag’, array(‘tag’ => ‘div’, ‘id’ => ‘but­ton­sSub­Form’, ‘class’ => ‘span-7 push-3 prepend-top’)),
))
->setElementDecorators(array(
array(‘DijitElement’),
))
->addElements(array(
new Zend_Dojo_Form_Element_Button(‘submit’, array(
’type’ => ‘but­ton’,
’label’ => ‘Sub­mit it!’,
’attribs’ => array(‘disabled’ => ‘dis­abled’),
)),

new Zend_Dojo_Form_Element_Button(‘cancel’, array(
’label’ => ‘Can­cel’,
)),
));
$subForm->submit->removeDecorator(‘DtDdWrapper’);
$subForm->cancel->removeDecorator(‘DtDdWrapper’);
return $sub­Form;
} // end but­ton­sSub­Form();
} // end class

That’s it. There may be some errors in there, but the idea is there. As you can see, once you see you how it goes together, it’s actu­ally triv­ial to add dijit lay­out con­tain­ers to a zend form. Be sure that you are in fact extend­ing Zend_Dojo_Form, or using one of the other meth­ods shown in the docs for dojo-enabling your form. You may or may not like the but­tons sub­Form I use here either…I do this because oth­er­wise the sub­mit but­tons have to be in their own tab, and that makes it con­fus­ing for some of my users…they don’t know where the but­tons are to sub­mit the form. So I dis­play them in a div that ren­ders out­side the tab con­tainer, dis­able the sub­mit but­ton, and using dojo’s meth­ods, I enable the but­ton on the onVa­lid­Stat­e­Change dojo event. Let me know if you have any questions!

regards,
Nathan Garlington

Tags: , , ,

Leave a Comment

*

Get Adobe Flash playerPlugin by wpburn.com wordpress themes