ZF-6741: Decorators and id attribute


In the default element decorators, the id attribute is set for the wrapping dd tag

                ->addDecorator('HtmlTag', array('tag' => 'dd',
                                                'id'  => $this->getName() . '-element'))

getName() should be replaced by getId() because the name of an element is not always unique.

The same issue appears in the DtDdWrapper decorator where the element name is taken for the ids.


Simply replacing getName() with getId() has not the effect you wish it to have, because the Id gets assembled only when rendering within FormElements Decorator.

So i am suggesting this solution, please give me your opinions.

    public function loadDefaultDecorators()
        if ($this->loadDefaultDecoratorsIsDisabled()) {

        $decorators = $this->getDecorators();
        if (empty($decorators)) {
            $getId = create_function('$decorator',
                                     'return $decorator->getElement()->getId()
                                             . "-element";');
                ->addDecorator('Description', array('tag' => 'p', 'class' => 'description'))
                ->addDecorator('HtmlTag', array('tag' => 'dd',
                                                'id'  => array('callback' => $getId))
                ->addDecorator('Label', array('tag' => 'dt'));

    protected function _htmlAttribs(array $attribs)
        $xhtml = '';
        $enc   = $this->_getEncoding();
        foreach ((array) $attribs as $key => $val) {
            $key = htmlspecialchars($key, ENT_COMPAT, $enc);
            if (is_array($val)) {
                if (array_key_exists('callback', $val)
                    && is_callable($val['callback'])) {              //added is_callable in r21970
                    $val = $val['callback']($this);
                else {
                    $val = implode(' ', $val);
            $val    = htmlspecialchars($val, ENT_COMPAT, $enc);
            $xhtml .= " $key=\"$val\"";
        return $xhtml;

Fix For SubTask ZF-7663

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

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

Solution of first comment applied to trunk r21964, sheduling for next minor release.