0

我有一些表单由于某些原因必须在不同的模块引导程序中实例化。但是在我想使用的那些形式中

$this->setAction($this->getView()->url(array('controller' => 'foo', 'action' => 'bar')));

在构造函数中。但是,由于我在引导程序中,因此无法访问 viewhelper url,所以还有什么办法吗?我现在得到的是

Fatal error: Uncaught exception 'Zend_Controller_Router_Exception' with message 'Route default is not defined'

在那条线上。我没有自定义路由,所以我只使用默认路由器和路由。

4

3 回答 3

2

如果您真的需要这么早实例化表单,那么可能一种可能性是您的 Bootstrap 中的初始化顺序。

在实例化表单的 Bootstrap 方法中,只需确保首先引导视图。然后在实例化过程中抓取视图对象并将其传递给表单。

就像是:

protected function _initForms()
{
    $this->bootstrap('view');
    $view = $this->getResource('view');
    $form = new My_Form($view);
    // ...        
}

然后在您的表单中,您可以执行以下操作:

public function __construct($view)
{
    $this->setView($view);
}

public function init()
{
    // set the action
    $this->setAction($this->getView()->url(array(
         'controller' => 'foo',
         'action'     => 'bar',
    )));

    // create your form elements
    // ...

}

怎么想?

于 2010-12-09T14:02:54.400 回答
0

我决定改变我的方法,让管理所有表单的类接受表单字符串而不是表单。

static public function registerForm($formClassName, $formName) {
  self::$_forms[$formName] = $formClassName;
}

然后我创建了函数来从类中获取表单或所有表单,如下所示

  public function getForm($name) {
    if(empty(self::$_forms[$name])) {
      throw new Core_Form_Store_Exception;
    }
    // If the for is instanciated we return it
    else if(self::$_forms[$name] instanceof Zend_Form) {
      return self::$_forms[$name];
    }
    else {
      // Instanciate the class and return it
      $form = new self::$_forms[$name];
      self::$_forms[$name] = $form;
      return $form;
    }
  }

getForms()只要求getForm()每个可用的表格。当涉及到函数的名称和传递的顺序参数时,我有点厌倦了模仿普通 Zend_Form 的接口。现在我让它工作了,作为奖励,我实际上并没有实例化任何形式,直到我需要它。如果我只从 store 类中请求一个特定的表单,那么只有该表单被实例化并保存以供将来访问。

于 2010-12-09T15:23:18.887 回答
0

在呈现表单时如何从视图中设置操作,例如

<?php echo $this->form->setAction($this->url(array(
          'controller' => 'foo',
          'action'     => 'bar'
      ))) ?>
于 2010-12-10T01:59:41.583 回答