2009年3月6日星期五

Re: [fw-mvc] how to pass params to a Zend_Form construction

I'm using Form in controller to apply a DRY (Don't Repeat Yourself) principle. So, I see form more like a refactor of controller code and thus would set Form in the Controller part of MVC.
 
What I have done is simple. I just override the default constructor with mine. In it, i call the parent one.
public __construct($myparam, $options)
{
  parent:__construct($options);
  // do someting with $myparam (database request to fill a Zend_Form_Select element for example).
  // this is code that i would have put in the controller when building the form if do not refactor in a Zend_Form subclass
}
 
Sébastien
2009/3/6 Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
2009/3/6 Van H Tran <tvhoang1980@yahoo.com>
I need to pass a variable (root categories) from controller to a form creation so that inside the form, the category tree can be generated

I am facing a problem here: How to pass the $rootCategoryId to the Form creation. I don't want to have to define a "setRootCategoryId()" and "addCategorySelectBox" functions inside the form. It does not seem so smooth, architecturally...

I think this is not a simple question.
What I do is defining setXXXTree()|setXXXService() on the form, that when called update the correspondent input (select I guess) basing on the business service passed. This way I can test the form in isolation passing in fake services (that return, for example, fake tags for a select tags[] of an article). Then you can write a factory to build the form passing in the right services basing on the root id you define or what ever. In your controller instance the factory and tell it to build a form for you.
You don't need a factory for every form, however; one for every module is enough, that collects creation methods for all entities form.



--
Giorgio Sironi
Piccolo Principe & Ossigeno Scripter
http://ossigeno.sourceforge.net

没有评论: