Page 1 of 1

Populating Zend_Form_Element_Select options, best practise

Posted: Thu May 26, 2011 4:54 pm
by marty pain
Hey,

Up until about 5 minutes ago I have always populated Zend_Form_Element_Select objects (addMultiOption) in the init() method of the class that extends Zend_Form. All of a sudden it feels a bit wrong and I think I should do that in the controller as it's a better separation of concerns. However this makes the controllers a little bigger so thought I would check here and see what people think.

All comments welcome!!

Re: Populating Zend_Form_Element_Select options, best practi

Posted: Sat May 28, 2011 5:30 pm
by josh
I use one of two techniques, depending on level of re-use that is desired.

1 - form class "adds" select boxes [Zend_Form_Element_Select] to itself, and adds options to each select box (all within the init method)

Now method two is for more re-use
2 - form class "adds" select box to itself (select box will be of class My_Form_Element_Select which extends the regular Zend select box' class). Within this select box's class, in it's init method, that is where the options are defined.

Method two allows the select box including it's options to be re-used in more than one form. The controller is definitely a bad place to put it in my opinion. The controller should have the least amount of code/details possible. The controller ideally should just call $form->isValid() and update the view.