繁体   English   中英

Zend 框架 - 表单不呈现

[英]Zend framework - form not rendering

我刚刚开始使用 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() ,但什么也没出现……会是什么?

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

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

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

确保将表单从 controller 传递到视图。

在您的动作处理程序中:

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

在您看来:

echo $this->form;

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

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

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

形式

 <?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

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

我遇到了这样的问题(完全相同的形式,因为它是 eclipse 示例)
我的问题是由于误解。 因为我认为我必须直接访问视图脚本。 我在浏览器中输入了类似的内容:hostname/application/view/script/something.php
但在 zend 中,所有访问都应该通过公用文件夹。 您必须像这样访问视图:hostname/app_name/public/guestbook
希望对你有帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM