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

Description

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:


        $this->setElementDecorators(array(
            array('ViewHelper'),
            array('HtmlTag', array('tag' => 'dd', 'class' => 'half')),
            array('Label', array('tag'=> 'dt', 'class' => 'half')),
        ));

It should generate:


name

instead of:


name

It's documented in http://framework.zend.com/manual/en/… docs.

Fix:


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

Comments

Please evaluate and categorize as necessary.

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.

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

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

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 :


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

$label = $element->getLabel();

=> this returns the tranlated value of the label

then

 
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 ?

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?

@Toby Use Zend_Form_Decorator_Label::setTagClass() or www.zfforum.de/showthread.php?t=7813" rel="nofollow">this tutorial in German.