代码之家  ›  专栏  ›  技术社区  ›  black sensei

Zend ViewScript装饰大图,整体使用。如何?

  •  1
  • black sensei  · 技术社区  · 15 年前

    的确,我在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

    2 回复  |  直到 15 年前
        1
  •  0
  •   tawfekov    15 年前

    嗨,我想重写您的操作,让我们暂时忘记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_form元素 窗体、显示组和子窗体 是元素的集合。整齐 为了呈现这些元素,它们使用 FormElements装饰器,其中 遍历所有项,调用 在每个上的render()并将它们与 已注册的分隔符。它可以 附加或预先附加内容 传递给它。

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

    我希望我做得对,仅供参考,我还没有测试这个动作:)

        2
  •  0
  •   black sensei    15 年前

    好的,我已经知道了。编码方面的一切都是正确的。
    我只需要把customerForm.phtml放到 /modules/booking/views/scripts/ 就这样了。谢谢那些帮助我的人。我现在可以学习如何定制标准的装饰,就像塔菲科夫试图解释的那样。