Zend Form:将 var 从控制器传递到表单

发布于 2024-11-27 15:00:21 字数 942 浏览 0 评论 0原文

我有这个问题:我想向我的表单传递一个参数,因为我需要它来完成选择。

这是我的代码

控制器:

    $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);
     ...

表单

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
     }

谢谢

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 = new 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看轻我的陪伴 2024-12-04 15:00:21

您可以执行以下操作:

如果您在表单中定义了构造函数,请添加“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
 }

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
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文