简体   繁体   中英

Pass variable into Zend Form

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

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...
}

您应该能够访问表单内的id属性

$this->_attribs['id']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM