Zend Form - Set class on dt label

Update. I managed to get this to work by creating my own Label decorator, which extended Zend / Form / Decorator / Label.php. I added the setTagClass () method to it and redefined the rendering method to create a wrapper tag with the required class. There may be a more elegant way to do this, but it seems to work.

I am looking for information on how to set a class on a label dt element using a decorator. The third line of code sets the class on the label and wraps the label in the dt tag. I want to know how I can set a class in a dt tag.

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

This produces output, for example

<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>

I want it to look like

<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>
+5
source share
3 answers

Label, tagClass!

:

$element->addDecorators(array( 
'ViewHelper', 
'Description',
'Errors',
array('HtmlTag', array('tag' => 'dd', 'class' => $class )),
array('Label', array('tag' => 'dt', 'class' => $class, 'tagClass' => $class))
));
+5

, . :

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

This is probably a cleaner way to do this, but here is my custom decorator for this (note: you need to extend the Zend_Form_Decorator_Label class):

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

/**
 * Set HTML tag 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 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;

    }
}   
0
source

All Articles