2013-04-23 84 views
1

我對Zend_Form默認呈現表單元素的方式感到不滿,並且想要通過實例化助手類生成的所有表單都覆蓋它我從Zend_Form繼承來處理一些我在所有表單上做的事情。用自定義HTML模板覆蓋默認的Zend_Form元素渲染器?

我想做的更改似乎比裝飾器的合理/可能更復雜,所以我想使用自定義HTML模板來實現這一點,我可以將表單值插入到自定義HTML代碼段中。

如何設置由我的類呈現的所有HTML元素以使用HTML模板?我應該從模板中調用哪些屬性/函數來獲取Zend_Form默認呈現的內容?最後,我寧願這樣做,而不必在我的代碼中創建的每個輸入元素上手動設置模板。

回答

1

我結束了使用自定義的觀點,我泛化,以任意形式工作。

使用這種方法,我能夠做以下事情:在一個DIV

  • 所需的表單元素的標籤後添加一個星號
  • 組輸入和錯誤在一起,這樣,當我漂浮標籤的左側東西還是排隊
  • 添加一個特殊的類來示數輸入,這樣我就可以凸顯他們
  • 更改某些錯誤信息,包括該元素的名稱,而不是「價值」
  • 傳遞文字註釋與表單元素沿下輸入要顯示
  • 在特殊元素

有些事情是不可能沒有viewscript,有的只是一個痛苦的不換標籤和輸入實行。我認爲這個解決方案對我而言將會更加靈活。

在我的輔助類render()功能:

$view = new Zend_View(); 
$view->setBasePath(SRC_ROOT . "/templates/forms"); 
$this->setDecorators(array(array('ViewScript', array('viewScript' => 'viewscript.php')))); 

這是我的viewscript:

<link rel="stylesheet" type="text/css" href="/styles.css" /> 

<form id="<?php echo $this->element->html_id ?>" class="<?php echo $this->element->html_class ?>" enctype="application/x-www-form-urlencoded" action="" method="post"> 
    <?php foreach($this->element as $element) { ?> 
     <?php 

     $decorators = $element->getDecorators(); 
     if(isset($decorators["Zend_Form_Decorator_Label"])) { 
      $label = $element->getLabel(); 
     } else { 
      $label = ""; 
     } 

     if($element->isRequired() === true) { 
      $label .= " *"; 
     } 
     ?> 
     <label class="label" for="<?php echo $element->getName(); ?>"><?php echo $label; ?></label> 

     <div class="formInput"> 
      <?php 
      // Add the error class to make the form inputs highlight in red 
      if($element->hasErrors()) { 
       $attribs = $element->getAttribs(); 
       if(!isset($attribs["class"])) { 
        $attribs["class"] = ""; 
       } 
       $attribs["class"] .= " inputError"; 
       $element->setAttribs($attribs); 
      } 

      // Print the input using Zend_Form's own mechanisms 
      $element->setDecorators(array('ViewHelper')); // Removes all decorators (labels, etc.) 
      $v = new Zend_View(); 
      $element->setView($v); 
      echo $element->render(); 

      if(isset($element->note)) { 
       echo "<p>{$element->note}</p>"; 
      } 

      // Print the error messages 
      if($element->hasErrors()) { 
       $errors = $element->getMessages(); 
      ?> 
       <ul class="errors <?php echo sizeof($errors) == 1 ? "noDecorations" : "" ?>"> 
       <?php 
       foreach($errors as $error => $message) { 
        // Custom error messages 
        if($error === "isEmpty") { 
         $message = $element->getLabel() . " cannot be empty"; 
        } ?> 
        <li><?php echo $message ?></li> 
       <?php } ?> 
       </ul> 
      <?php } ?> 
     </div> 
     <div style="float: clear;"></div> 
    <?php } ?> 
</form> 
1

你可以用自己的Custom_Form類擴展defualt Zend_Form類。在init()方法中覆蓋默認的元素修飾器。在這裏我的代碼片段:

//class Custom_Form extends Zend_Form 
public function init() 
{ 
    $this->setElementDecorators(
      array(array('ViewScript', array('viewScript' => '/controller_name/forms/fields/input-text.phtml'))), 
      array('email', 'firstname', 'lastname') 
     ); 
} 
+0

是否有我應該怎麼寫那個人,通用表單元素工作viewscripts任何文件?我所看到的全部是如何爲整個表單編寫一個viewscript,這不是我所追求的。我喜歡我可以將元素對象傳遞給Zend_Form,並且它可以呈現沒有我爲每個我需要的表單編寫HTML的東西,我只是希望它呈現不同的東西*。 – spiffytech 2013-04-24 13:29:24

+0

搜索setElementDecorators() – konradwww 2013-04-24 14:00:29