ZF-10065: Radio Element adds a Label decorator automatically

Description

When using radio element with a custom decorator (for example view script decorator) and you do not set the disableLoadDefaultDecorators flag to true the element adds a label decorator to the element.

This is not consistent with how other elements work, and in my case it causes the element to render an extra label decorator.

Commit 22151 in the Subversion changed the way Zend_Form_Element_Radio loads decorators.

What has changed can be seen here: http://framework.zend.com/code/browse/… and the commit message refers to the issue ZF-9682.

Comments

Adding a simple check if there are decorators set will fix the issue.

Also the @return type is wrong. Should be Zend_Form_Element_Radio.



    /**
     * Load default decorators
     *
     * Disables "for" attribute of label if label decorator enabled.
     *
     * @return void
     */
    public function loadDefaultDecorators()
    {
        if ($this->loadDefaultDecoratorsIsDisabled()) {
            return $this;
        }

        /**
         * Check if there are decorators set
         */
        $decorators = $this->getDecorators();
        if (!empty($decorators)) {
            return $this;
        }

        parent::loadDefaultDecorators();
        $this->addDecorator('Label', array('tag' => 'dt',
                                           'disableFor' => true));
        return $this;
    }

Why not check for a label decorator and if there is one set the disableFor attribute to true. If there isn't a label then add the default one as normal and set the disableFor.

 

As an additional problem this also overwrites any changes you make such as to the requiredSuffix and optionalSuffix.

In fact could the whole method be changed to the following:

public function loadDefaultDecorators() { parent::loadDefaultDecorators();

    if (!($label = $this->getDecorator('Label'))) {
        return $this;
    }

    $label->setOption('disableFor', true);

    return $this;
}````

The parent::loadDefaultDecorators(); takes care of the first if() in the new method itself. Then you just check for a label and disable the for option.