2010-03-04 26 views
5

Cập nhật Tôi có thể làm việc này bằng cách tạo trang trí nhãn tùy chỉnh, mở rộng Zend/Form/Decorator/Label.php. Tôi đã thêm một phương thức setTagClass() vào nó và overrode phương thức render để tạo thẻ kèm theo với lớp mong muốn. Có thể có một cách thanh lịch hơn để làm điều đó nhưng điều này dường như làm việc.Biểu mẫu Zend - Đặt lớp học trên nhãn dt

Tôi đang tìm thông tin về cách đặt lớp trên phần tử dt của nhãn bằng cách sử dụng trang trí. Dòng mã thứ ba bên dưới đặt lớp trên nhãn và kết thúc nhãn trong thẻ dt. Tôi muốn biết làm thế nào tôi có thể thiết lập các lớp học trên thẻ dt.

$txtLangPrefOther = $this->createElement('text','langPrefOther'); 
$txtLangPrefOther->setLabel('Language Preference Other:')); 
$txtLangPrefOther->getDecorator('Label')->setOptions(array('tag' => 'dt', 'class' => 'other')); 

này tạo ra như

<dt id="langPrefOther-label"> 
    <label for="langPrefOther" class="other">Language Preference Other:</label> 
</dt> 

<dd id="langPrefOther-element"> 
    <input type="text" id="langPrefOther" name="langPrefOther" "> 
</dd> 

Tôi muốn nó trông giống như

<dt id="langPrefOther-label" class="other"> 
    <label for="langPrefOther">Language Preference Other:</label> 
</dt> 

<dd id="langPrefOther-element"> 
    <input type="text" id="langPrefOther" name="langPrefOther" "> 
</dd> 

Trả lời

4

Bởi vì đó là một trang trí dạng không yếu tố trang trí. Hãy thử điều này:

$this->setDecorators(
array(
'FormElements', 
array('HtmlTag', array('tag' => 'dl', 'class' => 'theclass')), 
'Form' 
)); 
+0

Tôi chỉ muốn của một số dt để có bộ lớp 'khác', không phải tất cả trong số họ. – Chris

+1

Vì vậy, bạn có thể sử dụng jquery. $ ('# idOfFormElement'). Gần nhất ('dt'). AddClass ('className'); – cnkt

+2

Hoặc bạn có thể làm $ form-> element-> setDecorator ('HtmlTag', mảng ('tag' => 'dl', class => 'theclass')) –

0

Có lẽ một cách sạch hơn để làm điều này, nhưng đây là trang trí tùy chỉnh của tôi để thực hiện điều này (Lưu ý: bạn cần phải mở rộng các lớp Zend_Form_Decorator_Label):

/** 
* Class for HTML tag surrounding label 
* @var string 
*/ 
protected $_tagClass; 

/** 
* Set HTML tag's class 
* 
* @param string $tag 
* @return Zend_Form_Decorator_Label 
*/ 
public function setTagClass($tagClass) 
{ 
    if (empty($tagClass)) { 
     $this->_tagClass = null; 
    } else { 
     $this->_tagClass = (string) $tagClass; 
    } 

    $this->removeOption('tagClass'); 

    return $this; 
} 

/** 
* Get HTML tag's class, if any 
* 
* @return void 
*/ 
public function getTagClass() 
{ 
    if (null === $this->_tagClass) { 
     $tagClass = $this->getOption('tagClass'); 
     if (null !== $tagClass) { 
      $this->removeOption('tagClass'); 
      $this->setTagClass($tagClass); 
     } 
     return $tagClass; 
    } 

    return $this->_tagClass; 
} 

/** 
* Render a label 
* 
* @param string $content 
* @return string 
*/ 
public function render($content) 
{ 
    $element = $this->getElement(); 
    $view = $element->getView(); 
    if (null === $view) { 
     return $content; 
    } 

    $label  = $this->getLabel(); 
    $separator = $this->getSeparator(); 
    $placement = $this->getPlacement(); 
    $tag  = $this->getTag(); 
    $tagClass = $this->getTagClass(); 
    $id  = $this->getId(); 
    $class  = $this->getClass(); 
    $options = $this->getOptions(); 


    if (empty($label) && empty($tag)) { 
     return $content; 
    } 

    if (!empty($label)) { 
     $options['class'] = $class; 
     $label = $view->formLabel($element->getFullyQualifiedName(), trim($label), $options); 
    } else { 
     $label = '&#160;'; 
    } 

    if (null !== $tag) { 
     require_once 'Zend/Form/Decorator/HtmlTag.php'; 
     $decorator = new Zend_Form_Decorator_HtmlTag(); 
     $decorator->setOptions(array('tag' => $tag, 
            'id' => $id . '-label', 
            'class' => $tagClass)); 

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

    switch ($placement) { 
     case self::APPEND: 
      return $content . $separator . $label; 
     case self::PREPEND: 
      return $label . $separator . $content; 

    } 
} 
5

Có tài sản của trang trí Label được gọi là tagClass!

Hãy thử điều này:

$element->addDecorators(array( 
'ViewHelper', 
'Description', 
'Errors', 
array('HtmlTag', array('tag' => 'dd', 'class' => $class)), 
array('Label', array('tag' => 'dt', 'class' => $class, 'tagClass' => $class)) 
)); 
Các vấn đề liên quan