Zend Form - Set class on a label's dt

forms, zend-form, zend-framework

Solution

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

Problem

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

Original source