Zend ViewScript Decorator的大图,整体用途如何?

时间:2010-09-25 13:49:48

标签: php zend-framework zend-form

确实,我在stackoverflow上看过很多例子,很多都来自谷歌搜索,但显然没有人能够看到事情如何相互影响,即使是手册本身也是如此。

我刚刚选择了Zend Framework(1.10.8),在创建表单时,我终于发现现在的ViewScript对我来说更容易配置,但事实并非如此。

我有一个module booking,其中包含UserControllercreateAction。在/application/modules/booking/views/scripts/user下,我有create.phtmlcustormerForm.phtml

根据我的理解,在所有内容结束时,我的表单及其渲染将显示在我的create.phtml中,因为在我的表单中将customerForm.phtml用于视觉并在创建视图中注入。< / p>

所以我继续创建了一个简单的表单

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上的整个内容装饰。

任何人都可以帮我一臂之力并分享他宝贵的经验吗?非常感谢您阅读本文。也许我会把它作为一个知道的教程:D

2 个答案:

答案 0 :(得分:0)

您好我想重写您的动作,让我们暂时忘掉customerForm.phtml

public function createAction(){
  $this->view->show = "Please Enter your Details";

  $form = new Hotel_Form_Entity();
  $form->setAction("/booking/user/create");
  $form->setElementDecorators(array('viewHelper', 'formElements')));
  $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);
    }}}

formElements装饰者做什么

  

Zend_Form_Decorator_FormElements   表单,显示组和子表单   是元素的集合。为了   为了渲染这些元素,他们利用了   FormElements装饰器,哪个   遍历所有项目,调用   每个渲染()并加入它们   注册的分隔符。它可以   要么附加要么添加内容   传递给它。

http://framework.zend.com/manual/en/zend.form.standardDecorators.html

我希望我做对了,因为我还没有测试过这个动作:)

答案 1 :(得分:0)

好的,我已经弄清楚了。编码方面的一切都是正确的 我只需将customerForm.phtml放入/modules/booking/views/scripts/就可以了。感谢那些试图帮助我的人。我现在可以学习如何自定义标准装饰器,就像Tawfekov试图解释一样。