Zend Form: передать var из контроллера в форму - PullRequest
1 голос
/ 03 августа 2011

У меня есть эта проблема: я хочу передать своей форме параметр, потому что он мне нужен для завершения выбора.

Вот мой код

Контроллер:

    $p1 = $this->getRequest()->getParam ( '1' );
    $p2 = $this->getRequest()->getParam ( '2' );
    $utentepost = new Application_Model_myMapper();
    $data = $utentepost->populateFormInsert($p1, $p2);
    $form = new Application_Form_myForm();
    $form->populate($data);
     ...

Форма

публичная функция 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
     }

Спасибо

1 Ответ

1 голос
/ 04 августа 2011

Вы можете сделать следующее:

если в вашей форме определен конструктор, добавьте "parent :: ..":

public function __construct($options = null)
{
    parent::__construct($options);
}

теперь передайте атрибуты как массив в вашу форму:

$form = new Application_Form_myForm(array('p1' => $p1, 'p2' => $p2));

внутри вашей формы:

 protected function _setSelect() {
   $p1 = $this->getAttrib('p1');
   ... call model/mapper to execute sql query
 }
...