doucan8246326 2011-01-20 12:56
浏览 6
已采纳

通过控制器将值传递给Zend_Form?

I have this contact form with a selectbox, where the values on the select box, depend on the user_id. So these values are selected from the DB and I need the user_id in my form.

What I tried is giving my form a public user_id; datamember, so that when I create a form I can do this:

 $form = new My_Form();
 $form->user_id = $theUserId;

Maybe I'm missing something, but I just can't get this to work. $theUserId does have a value in my Controller, but not in my form (Whenever I do $this->user_id in my form I get: Invalid parameter number: no parameters were bound)

Any ideas how I should do this?

  • 写回答

3条回答 默认 最新

  • drz49609 2011-01-20 18:37
    关注

    You need to move the sql query out of the forms init() method which gets called by the constructor (and before you get chance to set the user id). if the results of the sql query are only needed at render time (rather than at validation time) then you might override the forms render() method, do your sql query there and then call parent::render()

    
    /* My Form */
    
    public function render($view = null)
    {
        /* SQL here */
    
        return parent::render($view);
    }
    

    EDIT:

    Ok, here's what I would do:

    
    class My_Form extends Zend_Form
    {
        private $_userId = null;
    
        /* Getter and Setter for user id here */
    
        public function init()
        {
            ...
            $this->createElement('Select', 'nameOfTheSelectElem',
                array(
                    /* Don't add the multiOptions here */
                )
            );
        }
    
        private function _addMultiOptionsForMySelectElem()
        {
            /* SQL Query here */
            $this->nameOfTheSelectElem->setMultiOptions($resultsOfTheSQLQuery);
        }
    
        public function render($view = null)
        {
            $this->_addMultiOptionsForMySelectElem();
    
            return parent::render($view);
        }
    
        public function isValid($data)
        {
            $this->_addMultiOptionsForMySelectElem();
    
            return parent::isValid($data);
        }
    }
    
    

    So in the init method you can create the element, but don't add the options to the element at this point. Create a private method to do the sql query and add the options to the select element and then call the private method from the render method and the isValid method. Hope that's clearer now. Is it?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路