Zend Form: How to pass parameters into the constructor?
Asked Answered
F

2

7

I'm trying to test my form. It will be constructing other objects, so I need a way to mock them. I tried passing them into 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?

Fruitful answered 17/6, 2010 at 17:12 Comment(0)
L
13

A quick look at the sourcecode of Zend_Form shows the Exception is thrown in the __set() method. The method is triggered because you are assigning $_regionMapper on the fly when it doesn't exist.

Declare it in the class and it should work fine, e.g.

class Form_Event extends Zend_Form
{
    protected $_regionMapper;

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

See the chapter on Magic Methods in the PHP Manual.

Ladonnalady answered 17/6, 2010 at 17:32 Comment(0)
M
1

Zend_Form constructor looks for a specific pattern in method's names in your form. The pattern is setMethodName. the constructor calls the MethodName method and pass the parameter to it.

So you'll have 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) );

So instead of params you can use any other names ( of course if it doesn't already exists in Zend_Form ).

Moderate answered 20/3, 2015 at 13:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.