views:

614

answers:

1

Hi all,

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>
+1  A: 

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

$this->setDecorators(
array(
'FormElements',
array('HtmlTag', array('tag' => 'dl', class => 'theclass')),
'Form'
));
cnkt
I only want certain dt's to have the 'other' class set, not all of them.
Chris
So, you can use jquery. $('#idOfFormElement').closest('dt').addClass('className');
cnkt
Or you can do $form->element->setDecorator('HtmlTag', array('tag' => 'dl', class => 'theclass'))
michal kralik
Yes, jquery would work, but I'm looking to do it within the Zend_Form.
Chris
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.
Chris