(我跟着this post得到了如何在不使用装饰器的情况下使用Zend_Form的逻辑。)
在我创建Zend_Form对象的操作中,并在呈现它之前将表单分配给视图:
public function indexAction()
{
$form = new Zend_Form();
$form->setAction('process')
->setMethod('post');
$username = $form->createElement('text','username');
$username->addValidator('alnum')
->setRequired(true)
->addFilter('StringtoLower');
$description = $form->createElement('textarea','description');
$description->addValidator('alnum')
->setRequired(false)
->addFilter('StringtoLower');
$submit = $form->createElement('submit','Submit');
$form->addElement($username);
$form->addElement($description);
$form->addElement($submit);
$this->view->form = $form;
$this->view->render('myForm/index.phtml'); //myForm is the actionController
}
然后我将以下代码添加到它的视图脚本(index.phtml)
<form>
<table>
<tr>
<td><label>Username</label></td>
<td><?php echo $this->form->getElement('username')->renderElement(); ?></td>
</tr>
<tr>
<td><label>Description</label></td>
<td><?php echo $this->form->getElement('description')->renderElement(); ?></td>
</tr>
<tr>
<td></td>
<td><?php echo $this->form->getElement('submit')->renderElement(); ?></td>
</tr>
</table>
</form>
但是它出错了
消息:装饰者名称元素 不存在
我没有使用装饰器,我的代码有什么问题。
这是完整的自定义错误消息
Application error
Exception information:
Message: Decorator by name Element does not exist
Stack trace:
#0 [internal function]: Zend_Form_Element->__call('renderElement', Array)
#1 C:\Program Files\Zend\Apache2\htdocs\Demo1\application\views\scripts\myForm\index.phtml(5): Zend_Form_Element_Text->renderElement()
#2 C:\Program Files\Zend\Apache2\htdocs\Demo1\library\Zend\View.php(108): include('C:\Program File...')
#3 C:\Program Files\Zend\Apache2\htdocs\Demo1\library\Zend\View\Abstract.php(833): Zend_View->_run('C:\Program File...')
#4 C:\Program Files\Zend\Apache2\htdocs\Demo1\application\controllers\MyFormController.php(38): Zend_View_Abstract->render('myForm/index.p...')
#5 C:\Program Files\Zend\Apache2\htdocs\Demo1\library\Zend\Controller\Action.php(513): myFormController->indexAction()
#6 C:\Program Files\Zend\Apache2\htdocs\Demo1\library\Zend\Controller\Dispatcher\Standard.php(289): Zend_Controller_Action->dispatch('indexAction')
#7 C:\Program Files\Zend\Apache2\htdocs\Demo1\library\Zend\Controller\Front.php(946): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#8 C:\Program Files\Zend\Apache2\htdocs\Demo1\library\Zend\Application\Bootstrap\Bootstrap.php(77): Zend_Controller_Front->dispatch()
#9 C:\Program Files\Zend\Apache2\htdocs\Demo1\library\Zend\Application.php(335): Zend_Application_Bootstrap_Bootstrap->run()
#10 C:\Program Files\Zend\Apache2\htdocs\Demo1\public\index.php(27): Zend_Application->run()
#11 {main}
如何在不使用装饰器的情况下解决此问题
答案 0 :(得分:4)
您实际上需要删除默认装饰器,并添加ViewScript装饰器(可能是错误装饰器)然后渲染它,看一下this文章。
简单示例:
class Demo_Form extends Zend_Form {
public function init() {
$this->addElement('text', 'username', array( 'decorators' => array( 'ViewHeper', 'Errors' ) ));
$this->addElement('text', 'lastname', array( 'decorators' => array( 'ViewHeper', 'Errors' ) ));
$this->setDecorators(array( array('ViewScript' => array( 'script' => 'demoForm.phtml'))));
}
}
使用视图脚本(demoForm.phtml):
<form action="<?php echo $this->escape($this->form->getAction() ?>" method="<?php echo $this->escape($this->form->getMethod() ?>">
<table>
<tr>
<td>User Name:</td>
<td><?php echo $this->form->username; ?></td>
</tr>
<tr>
<td>Last Name:</td>
<td><?php echo $this->form->lastname; ?></td>
</tr>
</table>
</form>
您可以完全控制渲染元素的方式和位置,然后在最终视图(来自控制器的视图)上打印您的表单:form; ?&GT;将viewScript渲染到View上。
答案 1 :(得分:0)
function indexAction () {
$form = new Zend_form();
.....
$this->view->form = $form;
}
在此步骤之后,只需转到path / to / index.phtml 和
<%php echo $this->form;
希望这对你有用,就像我一样!
答案 2 :(得分:0)
从版本1.7开始,您可以渲染单个表单元素装饰器:
echo $form->forename->renderLabel();
或
echo $form->forename->renderErrors();
我认为你的代码试图找到一个名为element的装饰器,但没有成功。
您可以渲染&#39;元素&#39;像这样:
echo $form->forename->render($view);
话虽如此,我不认为这就是你想要的。因为您将获得默认的附加装饰器。
你可能想要:
echo $form->forename->renderViewHelper()