(按照this post到了如何用Zend_Form不使用裝飾的邏輯。)Zend_Form的與XTHML腳本(不使用裝飾)
創建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>
但它得到一個錯誤
消息:裝飾由name元素 不存在
我沒有用的裝飾,什麼是我的代碼的問題。
這是完整的自定義錯誤消息
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}
我該如何解決這個問題,而無需使用裝飾
權。當你不設置ViewScript裝飾器時,它希望你將表單渲染爲一個,而不是單獨的元素。 – jimyi 2009-09-03 04:15:54
我不想使用裝飾器,它太複雜了。使用這種方式時,任何人都會對這個錯誤有個想法。 「消息:裝飾者名稱元素不存在」。 我已經使用相同的視圖腳本(這是控制器)的HTML form.Is它會導致任何錯誤?謝謝你的回答 – neobeacon 2009-09-03 04:36:49
並且主題有拼寫錯誤。它必須是「帶有xhtml腳本的Zend_Form」.Sorry – neobeacon 2009-09-03 04:47:31