Zend Form - Set class on a label's dt

11,887

Solution 1

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

Solution 2

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

$this->setDecorators(
array(
'FormElements',
array('HtmlTag', array('tag' => 'dl', 'class' => 'theclass')),
'Form'
));
Share:
11,887
Chris
Author by

Chris

Updated on June 09, 2022

Comments

  • Chris
    Chris almost 2 years

    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>
    
  • Chris
    Chris about 14 years
    I only want certain dt's to have the 'other' class set, not all of them.
  • cnkt
    cnkt about 14 years
    So, you can use jquery. $('#idOfFormElement').closest('dt').addClass('className');
  • mike
    mike about 14 years
    Or you can do $form->element->setDecorator('HtmlTag', array('tag' => 'dl', class => 'theclass'))
  • Chris
    Chris about 14 years
    Yes, jquery would work, but I'm looking to do it within the Zend_Form.
  • Chris
    Chris about 14 years
    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.
  • khuss
    khuss over 13 years
    The code above has a syntax error. The string class should be quoted as shown here: array('HtmlTag', array('tag' => 'dl', 'class' => 'theclass'))
  • olive
    olive about 13 years
    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.
  • blacktie24
    blacktie24 almost 13 years
    @Ihnz, I posted the custom label decorator that I used to do this. Hope it helps.