2008年8月20日星期三

Re: [fw-mvc] Setting a custom parameter for a form

On 17/08/2008 08:39, "Gordon Ross" <gr306@ucs.cam.ac.uk> wrote:

> I have a sample form of:
>
> class DummyForm extends Zend_Form
> {
> protected $rowCount;
>
> public function setCount($newCount)
> {
> if ($newCount>0)
> $this->rowCount = $newCount;
> }
[snip]
> In my controller I do:
>
> $form = new DummyForm();
> $form->setCount('2');
> $this->view->form = $form;
>
>
> However, the form is being generated too soon and my call to $form->setCount
> is being ignored.
>
> What do I need to change for me to be able to set $rowCount correctly ?

The power of open source...

I went and read the source for Zend_Form

All I needed to change from my code above is:

$form = new DummyForm(array('count' => '10'));

Basically, the options processing in the constructor for Zend_Form, if it
finds a parameter that it doesn't recognise (e.g. "myoption"), it looks for
the function "setMyoption" (NOTE The initial capitalisation) and calls with
the parameter you specified.

GTG

没有评论: