开发者

Access views url function in Zend_Form

开发者 https://www.devze.com 2023-01-30 06:05 出处:网络
I have some Forms that for some reasons have to be instantiated in the different modules bootstraps. But in those forms I want to use

I have some Forms that for some reasons have to be instantiated in the different modules bootstraps. But in those forms I want to use

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

in the constructor. But since the viewhelper url is not accessible yet since I'm in the bootstrap, is there anyway around this? What I get now is

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

on that line. I have NO custom routes so I only use the default router an开发者_如何学Cd routes.


If you really need to instantiate the forms so early, then perhaps one possibility is the order of the initializations in your Bootstrap.

In the Bootstrap method where you instantiate your forms, just make sure that you bootstrap the view first. Then grab the view object and pass it to the form during instantiation.

Something like:

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

Then in your form, you can do something like:

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
    // ...

}

Whaddya think?


I decided to change my approach and made the class that manages all the forms accept strings of forms instead of the forms.

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

Then I created function to get a form or all forms from the class like so

  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() only calls getForm() for each available form. I tired to mimic the interface of the normal Zend_Form a bit when it comes to the names of functions and the order arguments are passed. Now I have it working and as a bonus I don't actually instantiate any form until I need it. And if I only request a specific form from the store class only that form is instantiate and saved for future access.


How about you set the action from the view when rendering the form, eg

<?php echo $this->form->setAction($this->url(array(
          'controller' => 'foo',
          'action'     => 'bar'
      ))) ?>
0

精彩评论

暂无评论...
验证码 换一张
取 消