【发布时间】:2011-12-09 05:35:44
【问题描述】:
我在解决如何在 Zend Framework 中使用自定义表单时遇到一些问题。
我遵循了各种指南,但似乎都没有。什么都没有渲染。
这是我尝试使用的代码片段(下面的所有代码都在默认模块中)。我已将代码简化为用于测试的单个输入。
applications/forms/One/Nametest.php
class Application_Form_One_Nametest extends Zend_Form {
public function init() {
$this->setMethod('post');
$name = new Zend_Form_Element_Text('name');
$name->setLabel('Box Name')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('Submit Message');
$submit->setAttrib('id', 'submitbutton');
$submit->setAttrib('class', 'bluebutton');
$this->addElements(array($name, $submit));
}
}
application/views/scripts/one/formlayout.phtml
<form action="<?= $this->escape($this->form->getAction()) ?>" method="<?= $this->escape($this->form->getMethod()) ?>">
<p>
Please provide us the following information so we can know more about
you.
</p>
<? echo $this->element->name ?>
<? echo $this->element->submit ?>
</form>
应用程序/控制器/IndexController.php
public function formtestAction() {
$form = new Application_Form_One_Nametest();
$form->setDecorators(array(array('ViewScript', array('viewScript' => 'one/formlayout.phtml'))));
$this->view->form = $form;
}
application/views/scripts/index/formtest.phtml
<h1>Formtest</h1>
<?
echo $this->form;
?>
以上代码不会抛出任何错误或渲染formlayout.phtml的任何部分,包括表单标签或p标签之间的文本。
谁能告诉我我做错了什么?
【问题讨论】:
-
一个常见的错误是在您的表单类中有一个不调用父构造函数的构造函数。这会导致空表单输出,因为它是最终调用您的
init()方法的父构造函数。只是大声思考......
标签: forms zend-framework