ZF-6871: Zend_Form_Decorator_Label: Unable to manually define ID, setID/setOption('id') does nothing

Description

Current Zend_Form_Decorator_Label does not allow to manually define ID to the label, as it currently sets the ID to elements name with a "-label" suffix.


$decorator = $element->getDecorator('Label');
$decorator->setId('testLabelWhateverID');            // Does nothing
$decorator->setOption('id', 'testLabelWhateverID');  // Does nothing

The following modification would allow the override of the ID when specified and still retain auto generation of the ID from the element name.


    public function render($content)
    {
        $element = $this->getElement();
        $view    = $element->getView();
        if (null === $view) {
            return $content;
        }

        $label     = $this->getLabel();
        $separator = $this->getSeparator();
        $placement = $this->getPlacement();
        $tag       = $this->getTag();
//      $id        = $this->getId();
        $id        = $this->getId() ? $this->getId() : $element->getName() . '-label';
        $class     = $this->getClass();
        $options   = $this->getOptions();


        if (empty($label) && empty($tag)) {
            return $content;
        }

        if (!empty($label)) {
            $options['class'] = $class;
            $label = $view->formLabel($element->getFullyQualifiedName(), trim($label), $options);
        } else {
            $label = ' ';
        }

        if (null !== $tag) {
            require_once 'Zend/Form/Decorator/HtmlTag.php';
            $decorator = new Zend_Form_Decorator_HtmlTag();
//          $decorator->setOptions(array('tag' => $tag,
//                                       'id'  => $this->getElement()->getName() . '-label'));
            $decorator->setOptions(array('tag' => $tag,
                                         'id'  => $id));

            $label = $decorator->render($label);
        }

        switch ($placement) {
            case self::APPEND:
                return $content . $separator . $label;
            case self::PREPEND:
                return $label . $separator . $content;
        }
    }

Comments

Easier to just use custom decorators. Although would be nice to have in the documentation that certain options/attributes don't work in various form decorators.

Fixed in trunk r21961 and 1.10 release branch.

Reverted release-1.10 merge r21962, and sheduling for next minor release.