Zend 框架 - 表单不渲染

发布于 2024-11-29 19:54:29 字数 439 浏览 0 评论 0原文

我刚刚开始使用 Zend Framework,并遵循最新版本 (1.11.10) 的快速入门文档。一切都很顺利,但是当我放置表单代码并运行应用程序时,表单没有呈现。我的代码完全类似于 http://framework.zend.com/ Manual/en/learning.quickstart.create-form.html

在视图上,我可以使用 var_dump($this->form); 转储表单 我尝试过 echo $this->form(), echo $this->form->render(),但什么也没有出现...是吗?

I'm just starting to use Zend Framework and was following the quick start documentation for the latest version (1.11.10). Everything was going just fine, but when I placed the form code and ran the application, the form did not render. My code is exactly like http://framework.zend.com/manual/en/learning.quickstart.create-form.html

On the view, I can dump the form just fine with var_dump($this->form);
I've tried echo $this->form(), echo $this->form->render(), but nothing appeared... What could it be?

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

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

发布评论

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

评论(4

-柠檬树下少年和吉他 2024-12-06 19:54:29

当 Zend 找不到元素的模板文件时,可能会出现此问题。查看以下代码:

 $element->setDecorators(array(
            array('ViewScript',
                array(
                    'viewScript' => 'directory/_input.phtml'
                )
            )
        ));

文件 _input.phtml 必须位于该控制器的正确文件夹中。否则 Zend 无法找到输入模板,无法成功呈现您的元素,也不会显示任何内容。

This problem can occur when Zend can't find the template file for an element. Look at following code:

 $element->setDecorators(array(
            array('ViewScript',
                array(
                    'viewScript' => 'directory/_input.phtml'
                )
            )
        ));

The file _input.phtml must be in the right folder for this Controller. Otherwise Zend can't find the template for input and can't successfully render your element and will not show anything.

不知所踪 2024-12-06 19:54:29

确保将表单从控制器传递到视图。

在你的操作处理程序中:

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

在你看来:

echo $this->form;

我怀疑这是导致你的问题的原因,因为如果你尝试回显一个不支持的参数,Zend Framework 不会抱怨。不存在。 (即 echo $this->some_fake_parameter 不会执行任何操作)

Make sure you pass the form to the view from the controller.

In your action handler:

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

In your view:

echo $this->form;

I suspected that this was the cause of your problem because Zend Framework doesn't complain if you try to echo a parameter that doesn't exist. (i.e. echo $this->some_fake_parameter won't do anything)

无敌元气妹 2024-12-06 19:54:29

好的,我尝试了你的代码,它对我来说没有问题。
这就是一切:

控制器

<?php
class IndexController extends Zend_Controller_Action
{
    public function myTestAction()
    {
        $form = new Form_Guestbook();

        // ... processing logics
    if($this->getRequest()->isPost())
    {
    if($form->isValid($this->getRequest()->getPost()))
    {
        var_dump($form->getValues());
    }
    }
        $this->view->assign('form', $form);
    }
}

表单

        <?php
        class Form_Guestbook extends Zend_Form
        {
            public function init()
            {
                // Set the method for the display form to POST
                $this->setMethod('post');

                // Add an email element
                $this->createElement('text', 'email', array(
                    'label'      => 'Your email address:',
                    'required'   => true,
                    'filters'    => array('StringTrim'),
                    'validators' => array(
                        'EmailAddress',
                    )
                ));

                // Add the comment element
                $this->addElement('textarea', 'comment', array(
                    'label'      => 'Please Comment:',
                    'required'   => true,
                    'validators' => array(
                        array('validator' => 'StringLength', 'options' => array(0, 20))
                        )
                ));

                // Add a captcha
                $this->addElement('captcha', 'captcha', array(
                    'label'      => 'Please enter the 5 letters displayed below:',
                    'required'   => true,
                    'captcha'    => array(
                        'captcha' => 'Figlet',
                        'wordLen' => 5,
                        'timeout' => 300
                    )
                ));

                // Add the submit button
                $this->addElement('submit', 'submit', array(
                    'ignore'   => true,
                    'label'    => 'Sign Guestbook',
                ));

                // And finally add some CSRF protection
                $this->addElement('hash', 'csrf', array(
                    'ignore' => true,
                ));
            }
        }
    ?>

查看

<?php echo $this->form->render(); ?>

可以在以下位置看到:http://yaconiello. com/index/my-test

如果这对您不起作用,您可能遇到配置错误。

Ok so i tried your code, and it worked for me no problem.
Here is everything:

Controller

<?php
class IndexController extends Zend_Controller_Action
{
    public function myTestAction()
    {
        $form = new Form_Guestbook();

        // ... processing logics
    if($this->getRequest()->isPost())
    {
    if($form->isValid($this->getRequest()->getPost()))
    {
        var_dump($form->getValues());
    }
    }
        $this->view->assign('form', $form);
    }
}

Form

        <?php
        class Form_Guestbook extends Zend_Form
        {
            public function init()
            {
                // Set the method for the display form to POST
                $this->setMethod('post');

                // Add an email element
                $this->createElement('text', 'email', array(
                    'label'      => 'Your email address:',
                    'required'   => true,
                    'filters'    => array('StringTrim'),
                    'validators' => array(
                        'EmailAddress',
                    )
                ));

                // Add the comment element
                $this->addElement('textarea', 'comment', array(
                    'label'      => 'Please Comment:',
                    'required'   => true,
                    'validators' => array(
                        array('validator' => 'StringLength', 'options' => array(0, 20))
                        )
                ));

                // Add a captcha
                $this->addElement('captcha', 'captcha', array(
                    'label'      => 'Please enter the 5 letters displayed below:',
                    'required'   => true,
                    'captcha'    => array(
                        'captcha' => 'Figlet',
                        'wordLen' => 5,
                        'timeout' => 300
                    )
                ));

                // Add the submit button
                $this->addElement('submit', 'submit', array(
                    'ignore'   => true,
                    'label'    => 'Sign Guestbook',
                ));

                // And finally add some CSRF protection
                $this->addElement('hash', 'csrf', array(
                    'ignore' => true,
                ));
            }
        }
    ?>

View

<?php echo $this->form->render(); ?>

can be seen on: http://yaconiello.com/index/my-test

If this isnt working for you, you may be having a configuration error.

场罚期间 2024-12-06 19:54:29

我遇到了这样的问题(完全相同的形式,因为它是 Eclipse 示例)
我的问题是由于误解造成的。因为我认为我必须直接访问视图脚本。我在浏览器中输入如下内容:hostname/application/view/script/something.php

但在 zend 中,所有访问都应该通过公用文件夹。您必须访问这样的视图:hostname/app_name/public/guestbook

希望对你有帮助

I had a problem like that (exact same form, since it is eclipse example)
My problem was due to misunderstanding. Since I thought that I have to directly access to the view script. I entered in the browser something like: hostname/application/view/script/something.php

But in zend all accesses should be through public folder. You have to access to the view like this: hostname/app_name/public/guestbook

hope that would help you

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