开发者

How to create instance to zend controller

I have a controller named class TestControl开发者_如何学运维ler which extends some Zend_Controller_Action. Now I would like to use create an instance of TestController in TestForms (a Zend_Form). I want to populate a Zend_Form_Element_Select dynamically. Please suggest how I can do this. Thanx in advance.


Where are you instantiating the form - is it in the controller? Instead of having the form call an action on the controller to dynamically get the values, you should look at setting the values on the form after it has been instantiated.

A quick and dirty way of doing that would be to grab the values in the controller and assign it to the element via:

$values = $db->query('query');
$element = $form->getElement('dynamicSelect');
$element->setValue($values);

Of course having DB queries to a table in your controller isn't exactly best practice... Per philistyne's suggestion, I use a a form builder class to build forms dynamically from my models. I have mappers for each model, and I pass in the mapper to the form builder class so it can dynamically populate my select elements.


A couple of things to try (passing a controller into a form or instantiating from within one is not recommended):

  1. Use a model to access the dynamic values you want to put into your Zend_Form_Element_Select.
  2. If the form is complex, create a form builder class to take care of, and separate out, the heavy lifting of the form construction.
  3. Create customised form elements by extending from Zend_Form_Element_(Radio, Select, etc etc) if you feel you need very fine control over the form element's construction/behaviour/appearance, but wish to be able to reuse that element elsewhere.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜