将变量传递给 Zend Form [英] Pass variable into Zend Form
本文介绍了将变量传递给 Zend Form的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个实例化的 Zend 表单
I have a zend form instantiated
$form = Form_Example();
现在我想将一个 ID 从我的控制器传递给我的表单.
Now I want to pass an ID from my controller to my form.
所以我这样做了:
$form = Form_Example(array('id' => $id));
在表单中我尝试通过调用它:
Inside the form I try to call it through:
$this->id
但它不在那里.
有人知道如何将那个 id 输入到表单中吗?
Anybody knows how to get that id into the form?
谢谢
推荐答案
确保你有元素的 setter,在你的例子中是 public function setId($id)
.Zend_Form 构造函数检查属性的setter方法是否存在,如果存在则调用它,否则它设置表单的属性,参见setAttrib($key, $value)
.
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)
.
最终的结果是这样的
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...
}
这篇关于将变量传递给 Zend Form的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文