开发者

Load Zend_Form from a view helper in Zend?

Is it possible to load a Zend_Form from a view helper? I'm using thise form in a login action method. But I also want this form to be visible on the navigation on every page (so without the login action actually being called yet), the post method of the form will send to the login action method.

I'm guessing it should be done with a view helper but I don't see how.

Any ideas?

I tried with this: my view helper开发者_StackOverflow中文版:

class Zend_View_Helper_LoginForm
{
    function getLoginForm(){
    $form = new Form_LoginForm();
    return $form;
    }
}

and I call it from my layout like this: <?php echo $this->form(); ?> but this doesn't work. (I'm able to call the same form through an action method though!)

In this case it gives me this error (which doesn't make sense because my helper is only 9 lines long):

Warning: Missing argument 1 for Zend_View_Helper_Form::form() in C:\xampplite\htdocs\zendpr\library\Zend\View\Helper\Form.php on line 44


Your view helper should extends the Zend_View_Helper_Abstract class and the method of the view helper must have the same name as the class :

class Zend_View_Helper_LoginForm extends Zend_View_Helper_Abstract
{
    function loginForm() {
        $form = new Form_LoginForm();
        return $form;
    }
}

and you call it like this in your view script :

echo $this->loginForm();

If you call :

echo $this->form();

You are using the view helper Zend_View_Helper_Form


Have your View_Helper extend Zend_View_Helper_Abstract and override the setView()

class Zend_View_Helper_XX extends Zend_View_Helper_Abstract {

    public $view;

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

Initialise the form in your controller action and set the form reference

// controller action code
$this->view->form = $form;

Then in the view helper you can reference the form via the view

// view helper code
$this->view->form;


class Zend_View_Helper_LoginForm extents Zend_Form {

    function getLoginForm(){

        $form = new Form_LoginForm();       

        return $form;
    }
} 

OR

$this->view->form=$form;

Both are going to return the form. The view form is more specific for view.

Add this into your phtml view file

echo $this->form();

To answer this question - Remove the Parenthetical

Should be echo $this->form;

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜