Populating Zend_Form_Element_Select options, best practise

Discussion for various published PHP frameworks, including Zend Framework, CodeIgniter, Kohana, CakePHP, Yii, Symfony, and others.

Moderator: General Moderators

Post Reply
marty pain
Forum Contributor
Posts: 105
Joined: Thu Jun 11, 2009 5:32 am
Location: Essex

Populating Zend_Form_Element_Select options, best practise

Post 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!!
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Populating Zend_Form_Element_Select options, best practi

Post 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.
Post Reply