从 Zend 中的视图助手加载 Zend_Form?

发布于 2024-10-09 16:18:24 字数 608 浏览 3 评论 0原文

是否可以从视图助手加载 Zend_Form?我在登录操作方法中使用此表单。但我也希望此表单在每个页面的导航中都可见(因此尚未实际调用登录操作),表单的 post 方法将发送到登录操作方法。

我猜应该用视图助手来完成,但我不知道如何做。

有什么想法吗?

我尝试过这个: 我的视图助手:

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

我从我的布局中调用它,如下所示: form(); ?> 但这不起作用。 (不过,我可以通过操作方法调用相同的表单!)

在这种情况下,它会给我这个错误(这没有意义,因为我的助手只有 9 行长):

警告:缺少参数 1对于 Zend_View_Helper_Form::form() 在 C:\xampplite\htdocs\zendpr\library\Zend\View\Helper\Form.php 第 44 行

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:

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

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

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

发布评论

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

评论(3

递刀给你 2024-10-16 16:18:24

您的视图助手应该扩展 Zend_View_Helper_Abstract 类,并且视图助手的方法必须与该类具有相同的名称:

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

并且您在视图脚本中这样调用它:

echo $this->loginForm();

如果您调用:

echo $this->form();

您正在使用视图助手 Zend_View_Helper_Form

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

感性不性感 2024-10-16 16:18:24

让您的 View_Helper 扩展 Zend_View_Helper_Abstract 并覆盖 setView()

class Zend_View_Helper_XX extends Zend_View_Helper_Abstract {

    public $view;

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

在控制器操作中初始化表单并设置表单引用

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

然后在视图助手中您可以通过视图引用表单

// view helper code
$this->view->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;
    }
} 

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

两者都会返回表单。视图形式对于视图来说更加具体。

将其添加到您的 phtml 视图文件中

echo $this->form();

要回答这个问题 - 删除括号

应该是
echo $this->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;

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