Pass variable into Zend Form
Asked Answered
M

2

5

I have a zend form instantiated

$form = Form_Example();

Now I want to pass an ID from my controller to my form.

So I did this:

$form = Form_Example(array('id' => $id));

Inside the form I try to call it through:

$this->id

But it isn't there.

Anybody knows how to get that id into the form?

Thanks

Monstrosity answered 5/10, 2010 at 11:11 Comment(1)
i think this link help carlowens.me/2010/04/…Pennoncel
H
13

Make sure you have setter for the the element, in your case public function setId($id). Zend_Form constructor checks if setter method exists for the property, if it exists then it is called, otherwise it sets the attribute of the form, see setAttrib($key, $value).

The end result will be something like this

class Application_Form_YourForm extends Zend_Form {

    /**
     * Id
     * @var <type> 
     */
    protected $_id = null;

    /**
     * Setter for ID
     * @param <type> $id 
     */
    public function setId($id){
        $this->_id = $id;
    }

    // Rest of your code...
}
Hunger answered 5/10, 2010 at 12:35 Comment(1)
Simple solution to a seemingly complicated issue. Thank you!Graniah
C
3

You should be able to access the id property inside the form with

$this->_attribs['id']
Convertiplane answered 5/10, 2010 at 15:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.