我在如何在Zend Framework中使用自定义表单时遇到一些问题。
我遵循了各种指南,但似乎都没有。什么都没有呈现。
以下是我尝试使用的代码位(下面的所有代码都在默认模块中)。我已将代码简化为测试的单个输入。
应用/形式/单/ 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));
}
}
应用/视图/脚本/一个/ 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;
}
应用/视图/脚本/索引/ formtest.phtml
<h1>Formtest</h1>
<?
echo $this->form;
?>
上面的代码不会抛出任何错误或渲染formlayout.phtml的任何部分,包括p标签之间的表单标签或文本。
有谁可以告诉我,我可能做错了什么?
答案 0 :(得分:4)
我认为问题是你的表单元素的装饰器。您应该将装饰器设置为ViewHelper和Error only。它至少对我有用。
以下是我使用的代码,它应该可以使用
应用/形式/ form.php的
class Application_Form_Form extends Zend_Form {
public function loadDefaultDecorators() {
$this->setDecorators(
array(
array(
'ViewScript',
array(
'viewScript' => 'index/formlayout.phtml',
)
)
)
);
}
public function init() {
$this->setAction('/action');
$this->setMethod('post');
$this->addElement('text', 'name', array(
'decorators' => array('ViewHelper', 'Errors')
));
}
}
应用/视图/脚本/索引/ formlayout.phtml
<form action="<?php echo $this->element->getAction(); ?>" method="<?php echo $this->element->getMethod(); ?>">
<div>
<label for="name">Box Name</label>
<?php echo $this->element->name; ?>
</div>
<input type="submit" value="Submit Message" id="submitbutton" class="bluebutton">
</form>
应用/视图/脚本/索引/ index.phtml
<!-- application/views/scripts/index/index.phtml -->
<?php echo $this -> form; ?>
应用/控制器/ IndexController.php
public function indexAction() {
$form = new Application_Form_Form();
$this -> view -> form = $form;
}
答案 1 :(得分:1)
这是一个非常简单的示例,可让您从this article改编。
表格: -
class Application_Form_Test extends Zend_Form
{
public function init()
{
$this->setMethod('POST');
$this->setAction('/');
$text = new Zend_Form_Element_Text('testText');
$submit = new Zend_Form_Element_Submit('submit');
$this->setDecorators(
array(
array('ViewScript', array('viewScript' => '_form_test.phtml'))
)
);
$this->addElements(array($text, $submit));
$this->setElementDecorators(array('ViewHelper'));
}
}
此处调用setDecorators()
,addElements()
和setElementDecorators()
的顺序非常重要。
视图脚本_form_test.phtml
可以随意调用,但它必须位于/views/scripts
中,以便渲染器可以找到它。
/views/scripts/_form_test.phtml
看起来像这样: -
<form id="contact" action="<?php echo $this->element->getAction(); ?>"
method="<?php echo $this->element->getMethod(); ?>">
<p>
Text<br />
<?php echo $this->element->testText; ?>
</p>
<p>
<?php echo $this->element->submit ?>
</p>
</form>
您实例化表单,将其传递给视图并像往常一样呈现它。此示例的输出如下所示: -
<form id='contact' action='/' method='post'>
<p>
Text<br />
<input type="text" name="testText" id="testText" value=""></p>
<p>
<input type="submit" name="submit" id="submit" value="submit"></p>
</form>
这应该足以让您开始创建自己的表单。
答案 2 :(得分:0)
通常,如果您在屏幕上看不到任何内容,则表示发生了某种错误。 也许你关闭了错误或者某些东西,也许不是。我只是想给你一些想法。
我唯一可以发现以下内容的地方。 在下面的代码中,您还必须在尝试打印元素时指定表单。
<form>
action="<?php $this->escape($this->element->getAction()) ?>"
method="<?php $this->escape($this->element->getMethod()) ?>" >
<p>
Please provide us the following information so we can know more about
you.
</p>
<?php echo $this->element->getElement( 'name' ); ?>
<?php echo $this->element->getElement( 'submit' ) ?>
</form>
正如vascowhite的代码所示,一旦你进入了viewscript,带有表单的变量就被称为元素。 viewscript装饰器使用partial来进行渲染,因此它在视图中使用不同的变量名创建自己的范围。
所以,虽然在原始视图中它被称为$ form,但在视图中你必须将它称为元素。
此外,也许是复制/粘贴加速,但您使用了<? ?>
标记而不是<?= ?>
或<?php ?>
标记。也许这会导致一些超出解析的错误,这就是你没有输出的原因。