2013-03-01 68 views
0

我需要的HTML表單象下面這樣:Zend:我如何使用setDecorators()創建自定義表單元素?

<form> 
<div class="es_form_txt">Name</div> 
<div class="es_form"><input type="text" value="" class="email1"></div> 
<div class="clear1"></div> 
<div class="es_form_txt">Gender</div> 
<div class="es_form"> 
    <input type="radio" value="" name=""> Male 
    <input type="radio" value="" name=""> Female 
    <input type="radio" value="" name=""> Other 
</div> 
<div class="clear1"></div> 
<div class="es_form_txt">Country Code</div> 
<div class="es_form"><input type="text" value="" class="email1"></div> 
<div class="clear1"></div> 
<div class="es_form_txt">Phone</div> 
<div class="es_form"><input type="text" value="" class="email1"></div> 
<div class="clear1"></div> 
<div class="clear1"></div> 
<div class="es_form">&nbsp;</div> 
<div class="es_form"><input type="button" class="button" value="Update"></div> 
<div class="clear"></div> 
</form> 

我試過各種解決方案,這裏沒有工作!任何人都可以指向正確的方向嗎?

回答

1

我發現當我需要使用Zend_Form時需要顯示特定的HTML時,最可靠的解決方案通常是使用ViewScript decorator

使用Zend_Form構建一個標準表單。

<?php 
//application/forms/Search.php 
class Application_Form_Search extends Zend_Form 
{ 
    public function init() 
    { 
     $this->setMethod('POST'); 
     //assign the view script to the decorator, this can also be done in the controller 
     $this->setDecorators(array(
      array('ViewScript', array(
        'viewScript' => '_searchForm.phtml' 
      )) 
     )); 
     // create new element 
     $query = $this->createElement('text', 'query'); 
     // element options 
     $query->setLabel('Search Keywords'); 
     // add the element to the form 
     $this->addElement($query); 
     //submit element 
     $submit = $this->createElement('submit', 'search'); 
     $submit->setLabel('Search Site'); 
     $submit->setDecorators(array('ViewHelper')); 
     $this->addElement($submit); 
    } 
} 

然後構建用於呈現表單的實際腳本。

<!-- ~/views/scripts/_searchForm.phtml --> 
<article class="search"> 
    <!-- set the action and the method --> 
    <form action="<?php echo $this->element->getAction()?>" method="<?php echo $this->element->getMethod()?>"> 
     <table> 
      <tr> 
       <!-- you can render individual decorators. --> 
       <th><?php echo $this->element->query->renderLabel()?></th> 
      </tr> 
      <tr> 
       <td><?php echo $this->element->query->renderViewHelper()?></td> 
      </tr> 
      <tr> 
       <!-- or you can render a complete element --> 
       <td><?php echo $this->element->search?></td> 
      </tr> 
     </table> 
    </form> 
</article> 

這目前呈現爲:

<article class="search"> 
    <form action="/video/index/display" method="post"> 
     <table> 
      <tr> 
       <th> 
        <dt id="query-label"> 
         <label for="query" class="optional">Search Keywords</label> 
        </dt> 
       </th> 
      </tr> 
      <tr> 
       <td> 
        <input type="text" name="query" id="query" value="" placeholder="Movie Title" size="20"> 
       </td> 
      </tr> 
      <tr> 
       <td> 
        <input type="submit" name="search" id="search" value="Search Video Collection!"> 
       </td> 
      </tr> 
     </table> 
    </form> 
</article> 

一些實驗將需要得到正是你所需要的輸出。

+0

這是偉大的,但我想知道如果它的良好做法,在我的視圖文件中有php代碼 - 我真的不喜歡它。無論如何,你的是一個快速解決方案:) – VishwaKumar 2013-03-01 10:26:07

+0

有沒有辦法避免在視圖文件中的PHP,這就是爲什麼他們是.phtml(PHP的HTML)。如果你願意的話,你可以使用一個單獨的模板系統,但是對於很少的獎勵來說這可能是一個很大的努力。 – RockyFord 2013-03-02 06:10:18

相關問題