Zend Form - Set class on a label's dt
Asked Answered
P

3

5

Update I was able to get this to work by creating a custom Label decorator, which extended Zend/Form/Decorator/Label.php. I added a setTagClass() method to it and overrode the render method to create the enclosing tag with the desired class. There might be a more elegant way to do it but this seems to work.

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

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

This produces output such as

<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>
Picrate answered 4/3, 2010 at 22:28 Comment(0)
L
5

There is a property of the Label decorator called tagClass!

Try this out:

$element->addDecorators(array( 
'ViewHelper', 
'Description',
'Errors',
array('HtmlTag', array('tag' => 'dd', 'class' => $class )),
array('Label', array('tag' => 'dt', 'class' => $class, 'tagClass' => $class))
));
Lauranlaurance answered 2/8, 2012 at 15:59 Comment(0)
G
4

Because it's a form decorator not element decorator. Try this:

$this->setDecorators(
array(
'FormElements',
array('HtmlTag', array('tag' => 'dl', 'class' => 'theclass')),
'Form'
));
Gyn answered 4/3, 2010 at 22:57 Comment(8)
I only want certain dt's to have the 'other' class set, not all of them.Picrate
So, you can use jquery. $('#idOfFormElement').closest('dt').addClass('className');Gyn
Or you can do $form->element->setDecorator('HtmlTag', array('tag' => 'dl', class => 'theclass'))Combinative
Yes, jquery would work, but I'm looking to do it within the Zend_Form.Picrate
It looks like the default Label decorator only allows you to enclose the label with one tag and only lets you set the id on that tag. At the time being, I don't see how to add a class to the tag without a custom Decorator. The jquery suggestion is a good workaround to the problem if you don't want to bother with a custom decorator.Picrate
The code above has a syntax error. The string class should be quoted as shown here: array('HtmlTag', array('tag' => 'dl', 'class' => 'theclass'))Vinitavinn
Chris, did you ever find out a way of doing this cleanly? I have exactly the same problem. I can't think of a decent way of doing it.Corrincorrina
@Ihnz, I posted the custom label decorator that I used to do this. Hope it helps.Palmieri
P
0

There's probably a cleaner way to do this, but here's my custom decorator to accomplish 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'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;

    }
}   
Palmieri answered 13/7, 2011 at 17:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.