Zend form: how to pass parameters to the constructor?

I'm trying to check my form. He will build other objects, so I need a way to mock them. I tried passing them to the constructor ...

class Form_Event extends Zend_Form { public function __construct($options = null, $regionMapper = null) { $this->_regionMapper = $regionMapper; parent::__construct($options); } 

... but I get an exception:

 Zend_Form_Exception: Only form elements and groups may be overloaded; variable of type "Mock_Model_RegionMapper_b19e528a" provided 

What am I doing wrong?

+6
php phpunit zend-framework zend-form
source share
2 answers

A quick search of the source code of Zend_Form shows that the Exception is __set() by the __set() method. The method runs because you assign $_regionMapper on the fly when it does not exist.

Declare it in the class and it should work fine, for example.

 class Form_Event extends Zend_Form { protected $_regionMapper; public function __construct($options = null, $regionMapper = null) { $this->_regionMapper = $regionMapper; parent::__construct($options); } 

See the Magic Methods chapter in the PHP Manual .

+11
source share

Zend_Form constructor looks for a specific template in the method names in your form. The setMethodName template. the constructor calls the MethodName method and passes it a parameter.

So you get this in your class:

 class My_Form extends Zend_Form { protected $_myParameters; public function setParams($myParameters) { $this->_myParameters = $myParameters; } 

And you pass the parameters to your form with:

 $form = new My_Form( array('params' => $myParameters) ); 

Therefore, you can use any other names instead of params (of course, if it does not already exist in Zend_Form ).

+1
source share

All Articles