Zend框架 - 表单不呈现

时间:2011-08-15 18:09:32

标签: php forms zend-framework render

我刚刚开始使用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(),但没有出现......它可能是什么?

4 个答案:

答案 0 :(得分:2)

当Zend找不到元素的模板文件时,可能会发生此问题。请看下面的代码:

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

文件_input.phtml必须位于此Controller的右侧文件夹中。否则,Zend找不到输入的模板,无法成功渲染元素,也不会显示任何内容。

答案 1 :(得分:1)

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

在您的操作处理程序中:

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

在您看来:

echo $this->form;

我怀疑这是您的问题的原因,因为如果您尝试回显不存在的参数,Zend Framework不会抱怨。 (即echo $this->some_fake_parameter将不会做任何事情)

答案 2 :(得分:1)

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

控制器

<?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 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 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, )); } } ?>

可以在http://yaconiello.com/index/my-test

上看到

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

答案 3 :(得分:0)

我有这样的问题(完全相同的形式,因为它是eclipse例子)
我的问题是由于误解造成的。因为我以为我必须直接访问视图脚本。我在浏览器中输入了:hostname / application / view / script / something.php
但是在zend中,所有访问都应该通过公共文件夹。您必须访问如下视图:hostname / app_name / public / guestbook
希望能帮到你