2012-04-27 3 views
1

私はZend FrameworkとZend_Formを使ってフォームをレンダリングしています。しかし、それをカスタマイズするのが難しいと感じたので、要素を個別に印刷することに決めました。グループディスプレイの内容を個別に印刷するにはどうすればいいですか?

問題は、表示グループ内の個々の要素の印刷方法がわかりません。私は表示グループ(フィールドセット)を印刷する方法を知っていますが、のように、float:leftをキャンセルする必要があります。

グループを内容なしで表示する方法はありますか?自分自身?

あなたの助けをいただき、ありがとうございます。

答えて

7

あなたが探していることは「ViewScript」デコレータです。それはあなたが必要とするどのような方法であなたのhtmlを形成することができます。ここでは、それがどのように動作するかの簡単な例であります:

フォーム、簡易検索フォーム:

あなたは、あなたがそれをしたいどのようにHTMLを構築するところ
<?php 
class Application_Form_Search extends Zend_Form 
{ 
    public function init() { 
     // create new element 
     $query = $this->createElement('text', 'query'); 
     // element options 
     $query->setLabel('Search Keywords'); 
     $query->setAttribs(array('placeholder' => 'Query String', 
      'size' => 27, 
      )); 
     // add the element to the form 
     $this->addElement($query); 
     //build submit button 
     $submit = $this->createElement('submit', 'search'); 
     $submit->setLabel('Search Site'); 
     $this->addElement($submit); 
    } 
} 

次は、これはここに、デコレータである「部分的」では次のようになります。

<article class="search"> 
<!-- I get the action and method from the form but they were added in the controller --> 
    <form action="<?php echo $this->element->getAction() ?>" 
      method="<?php echo $this->element->getMethod() ?>"> 
     <table> 
      <tr> 
      <!-- renderLabel() renders the Label decorator for the element 
       <th><?php echo $this->element->query->renderLabel() ?></th> 
      </tr> 
      <tr> 
      <!-- renderViewHelper() renders the actual input element, all decorators can be accessed this way --> 
       <td><?php echo $this->element->query->renderViewHelper() ?></td> 
      </tr> 
      <tr> 
      <!-- this line renders the submit element as a whole --> 
       <td><?php echo $this->element->search ?></td> 
      </tr> 
     </table> 
    </form> 
</article> 

、最終的にはコントローラコード:

public function preDispatch() { 
     //I put this in the preDispatch method because I use it for every action and have it assigned to a placeholder. 
     //initiate form 
     $searchForm = new Application_Form_Search(); 
     //set form action 
     $searchForm->setAction('/index/display'); 
     //set label for submit button 
     $searchForm->search->setLabel('Search Collection'); 
     //I add the decorator partial here. The partial .phtml lives under /views/scripts 
     $searchForm->setDecorators(array(
      array('ViewScript', array(
        'viewScript' => '_searchForm.phtml' 
      )) 
     )); 
     //assign the search form to the layout place holder 
     //substitute $this->view->form = $form; for a normal action/view 
     $this->_helper->layout()->search = $searchForm; 
    } 

表示通常の<?php $this->form ?>のビュースクリプトのこのフォーム。

この方法は、Zend_Formでビルドする任意のフォームに使用できます。したがって、自分のフィールドセットに要素を追加するのは簡単です。

+0

ありがとうございました、私が最終的にしたことです、私は私の視野で自分のフィールドセットを設定しました。ありがとう:) –

関連する問題