2010-02-09 69 views
1

我目前正在嘗試構建一個簡單的自定義圖層,我將擴展而不是Zend_Form。例如,My_Form。Zend_Form覆蓋元素默認爲自定義佈局

我希望我所有的窗體看起來都一樣,所以我在My_Form中設置它。這是迄今爲止。

class My_Form extends Zend_Form 
{ 
    protected $_elementDecorators = array(
     'ViewHelper', 
     'Errors', 
     array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'value_cell')), 
     array('Label', array('tag' => 'td')), 
     array(array('row' => 'HtmlTag'), array('tag' => 'tr')), 
    ); 
} 

而我所有的表格都會擴展這個。現在這個工作正常,問題出現在$ _elementDecorators數組中。我正在將標籤包裝在「td」中,Label Decorator將默認的「id」應用於該「td」,但我想要爲該「td」添加一個類。

無論如何要完成這個,這個數組?如果沒有,有沒有更好的方法來做到這一點?或者如果是這樣,有人可以向我描述這個數組是如何工作的嗎?

期望的結果:

<tr> 
    <td class='label_cell'> 
     <label /> 
    </td> 
    <td class='value_cell'> 
     <input /> 
    </td> 
</tr> 

謝謝。

回答

1

我找到了一個解決方案,雖然不知道它是最好的。

在這裏,我決定創建一個自定義裝飾器並加載它。

/** 
* Overide the default, empty, array of element decorators. 
* This allows us to apply the same look globally 
* 
* @var array 
*/ 
protected $_elementDecorators = array(
    'ViewHelper', 
    'Errors', 
    array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'value_cell')), 
    array('CustomLabel', array('tag' => 'td')), 
    array(array('row' => 'HtmlTag'), array('tag' => 'tr')) 
); 

/** 
* Prefix paths to use when creating elements 
* @var array 
*/ 
protected $_elementPrefixPaths = array(
    'decorator' => array('My_Form_Decorator' => 'My/Form/Decorator/') 
); 

裝飾:

class My_Form_Decorator_CustomLabel extends Zend_Form_Decorator_Label 
{ 
    public function render($content) 
    { 
     //... 
     /** 
     * Line 48 was added for the cutom class on the <td> that surrounds the label 
     */ 
     if (null !== $tag) { 
      require_once 'Zend/Form/Decorator/HtmlTag.php'; 
      $decorator = new Zend_Form_Decorator_HtmlTag(); 
      $decorator->setOptions(array('tag' => $tag, 
             'id' => $this->getElement()->getName() . '-label', 
             'class' => 'label_cell')); 

      $label = $decorator->render($label); 
     } 
     //... 
    } 
} 

雖然這工作得很好,我仍然好奇,如果有做這樣一個簡單的方法。

任何想法?

0

快速劈我用相同的問題當處理結束了使用:

class My_Form extends Zend_Form 
{ 
    protected $_elementDecorators = array(
     'ViewHelper', 
     'Errors', 
     array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'value_cell')), 
     array('Label', array('tag' => 'th')), 
     array(array('row' => 'HtmlTag'), array('tag' => 'tr')), 
    ); 
} 

的區別是:`陣列( '標籤',陣列( '標記'=> '' )),

所以你的「標籤」列有TH元素,而你的元素列有TD元素。

然後,您可以根據自己的喜好設計風格。