Zend Form : pass var from controller to form
i have this problem : i want to pass to my form a param because i need it to complete a select.
Here's my code
Controller :
$p1 = $this->getRequest()->getParam ( '1' );
$p2 = $this->getRequest()->getParam ( '2' );
$utentepost = new Application_Model_myMapper();
$data = $utentepost->populateFormInsert($p1, $p2);
$form = n开发者_JAVA百科ew Application_Form_myForm();
$form->populate($data);
...
Form
public function init() {
$this->setMethod('post');
$this->addElement('text', 'p1', array());
$this->addElement('text', 'p2', array());
$this->addElement('select', 'sede_id', array(
'label' => 'Sede',
'required' => true,
'multiOptions' => $this->_setSelect($p1),
));
.... ....
protected function _setSelect($p1) {
... call model/mapper to execute sql query
}
Thanks
You could do the following:
if you have an Constructor defined in your form add "parent::..":
public function __construct($options = null)
{
parent::__construct($options);
}
now pass the attribs as array to your form:
$form = new Application_Form_myForm(array('p1' => $p1, 'p2' => $p2));
inside your form:
protected function _setSelect() {
$p1 = $this->getAttrib('p1');
... call model/mapper to execute sql query
}
精彩评论