ZF-2992: Label decorator sets provided class for label tag instead of block level tag

Issue Type: Bug Created: 2008-03-28T13:48:24.000+0000 Last Updated: 2011-09-23T13:40:38.000+0000 Status: Resolved Fix version(s): Reporter: Wojciech Naruniec (wojciech) Assignee: Matthew Weier O'Phinney (matthew) Tags: - Zend_Form

Related issues: Attachments:


When option 'tag' is specified for Label decorator wraps label in that block level tag and if the 'class' option is provided it should set 'class' parameter for this block level tag. It doesn't work properly because it sets class for label element.

Sample code:

<pre class="highlight">
            array('HtmlTag', array('tag' => 'dd', 'class' => 'half')),
            array('Label', array('tag'=> 'dt', 'class' => 'half')),

It should generate:

<pre class="highlight">

instead of:

<pre class="highlight">

It's documented in… docs.


<pre class="highlight">
Index: Zend/Form/Decorator/Label.php
--- Zend/Form/Decorator/Label.php   (revision 9083)
+++ Zend/Form/Decorator/Label.php   (working copy)
@@ -296,14 +296,13 @@
         if (!empty($label)) {
-            $options['class'] = $class;
             $label = $view->formLabel($element->getName(), trim($label), $options); 
         if (null !== $tag) {
             require_once 'Zend/Form/Decorator/HtmlTag.php';
             $decorator = new Zend_Form_Decorator_HtmlTag();
-            $decorator->setOptions(array('tag' => $tag));
+            $decorator->setOptions(array('tag' => $tag, 'class' => $class));
             $label = $decorator->render($label);


Posted by Wil Sinclair (wil) on 2008-03-31T15:27:02.000+0000

Please evaluate and categorize as necessary.

Posted by Matthew Weier O'Phinney (matthew) on 2008-04-22T11:31:59.000+0000

That makes no sense. The Label tag can be generated without a wrapping HTML tag, and your patch would remove the class in such a situation.

The class associated with the label decorator should, and does, associate the provided class with the label tag itself.

Posted by Jean Rumeau (rumeau) on 2008-05-17T13:22:48.000+0000

im on this situation too, and as same as the element case i think this should be an option to the label too, because when setting HtmlTag on an element, i can set the class attribute to the new block wich contains the element, but i cant do the same with the label, maybe the Label decorator should provide a solution like HtmlTag does.

im aware that the fix posted here removes the option of setting the class on the label but as the label decorator makes use of the HtmlTag decorator, it should be possible to pass the class to the HtmlTag as an option right?

i think that would be a good solution..

Posted by Laura Dean (ldean) on 2009-06-15T11:37:55.000+0000

I'm in this situation, too, of wanting to add a class to the tag wrapping the label. As the OP metioned, the documentation implies that this is possible:

You may optionally specify a 'tag' option; if provided, it wraps the label in that block-level tag. If the 'tag' option is present, and no label present, the tag is rendered with no content. You can specify the class to use with the tag with the 'class' option or by calling setClass().

If this is not possible, at least change the documentation...

Posted by Devoir Nicolas (nicolasd) on 2010-08-09T23:35:46.000+0000

Hello I would like a precision on one part of the code. I m working on i18n and I ve got a problem when this decorator is translated :

<pre class="highlight">
public function getLabel()
        if (null === ($element = $this->getElement())) {
            return '';

        $label = $element->getLabel();
        $label = trim($label);

        if (empty($label)) {
            return '';

        if (null !== ($translator = $element->getTranslator())) {
            $label = $translator->translate($label);

        $optPrefix = $this->getOptPrefix();
        $optSuffix = $this->getOptSuffix();
        $reqPrefix = $this->getReqPrefix();
        $reqSuffix = $this->getReqSuffix();
        $separator = $this->getSeparator();

        if (!empty($label)) {
            if ($element->isRequired()) {
                $label = $reqPrefix . $label . $reqSuffix;
            } else {
                $label = $optPrefix . $label . $optSuffix;

        return $label;

<pre class="highlight">
$label = $element->getLabel();

=> this returns the tranlated value of the label


<pre class="highlight"> 
if (null !== ($translator = $element->getTranslator())) {
    $label = $translator->translate($label);

=> this returns the translation of the translated value of the label

Did I miss something or do I have got a double translation ?

Posted by Toby Davis (tdavis) on 2011-09-23T13:25:13.000+0000

This continues to be an issue in version 1.11, is there a work around for adding attributes to the tag wrapped around the label?

Posted by Frank Brückner (frosch) on 2011-09-23T13:40:38.000+0000

@Toby Use Zend_Form_Decorator_Label::setTagClass() or" rel="nofollow">this tutorial in German.

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.