的确,我在StackOverflow上看到过很多例子,在Google搜索上也看到过很多例子,但是很明显,没有人从一个大的画面上看到事物是如何彼此相依的,甚至是从手册本身。
我刚刚选择了Zend框架(1.10.8),当创建表单时,我终于发现现在的视图脚本对我来说更容易配置,但事实并非如此。
我有一个
module booking
用
UserController
和
createAction
在里面,在下面
/application/modules/booking/views/scripts/user
我有
create.phtml
和
custormerForm.phtml
.
据我所知,在所有内容的末尾,我的窗体及其呈现将显示在
创建.phtml
在我的表格中将使用
customerForm.phtml
因为它是可视的,并注入到创建视图中。
所以我创建了一个简单的表单
function init(){
$this->setMethod("post");
$name = New Zend_Form_Element_Text("name");
$name->setLabel("Name: ")
->setOptions(array("size"=>"35"))
->setRequired(true)
->addValidator("NotEmpty", true);
$surname = New Zend_Form_Element_Text("surname");
$surname->setLabel("Surname: ")
->setOptions(array("size"=>"35"))
->setRequired(true)
->addValidator("Alpha", true);
$this->addElement($name)
->addElement($surname)
->addElement($submit);
}
下面是UserController中的CreateAction
public function createAction(){
$this->view->show = "Please Enter your Details";
$form = new Hotel_Form_Entity();
$form->setAction("/booking/user/create");
//and here set the for to be displayed at described in customerForm view
$form->setDecorators(array(array('ViewScript',array('viewScript'=>'customerForm.phtml'))));
//so here i set the form to form variable accessible in create view
$this->view->form = $form;
if($this->getRequest()->isPost()){
if($form->isValid($this->getRequest()->getPost())){
$values = $form->getValues();
$this->_helper->flashMessenger("Thank you.Form processed");
$this->_forward("success","user","booking",$values);
}
}
}
下面是create.phtml和customerform.phtml
<!-- create.phtml -->
<h4><?php echo $this->show; ?></h4><br/><!-- -->
<p><?php echo $this->form; ?></p><br/>
<!-- customerForm.phtml -->
<div style="padding: 10 0 0 15; border: solid 1.5px #999">
<form action="<?php echo $this->element->getAction(); ?>" method="<?php echo $this->element->getMethod(); ?>">
<table>
<tr>
<td><?php echo $this->element->name; ?></td>
<td></td>
</tr>
<tr>
<td></td>
<td><?php echo $this->element->surname; ?></td>
</tr>
<tr>
<td colspan="2"><?php echo $this->element->submit; ?> </td>
</tr>
</table>
</form>
</div>
所以当我像在
http://localhost/project/booking/user/create
它只显示带有创建视图内容的布局,而不显示任何表单。页面源中没有任何内容,没有错误。
我是否对如何使用这个有错误的想法,或者我只是在代码中做了一些错误的事情?由于我使用的是Zend Framework 1.10.8,所以在ViewScript Decorator上似乎没有任何教程可以涵盖整个过程。
有人能帮我一把,分享一下他在这里的宝贵经验吗?非常感谢你阅读这篇文章。也许我会让这成为一个教程谁知道:d