ZF-8265: Zend_Form_Element_Radio generates non valid html

Description


        $this->addElement('radio', 'test', array(
            'label'    => 'test',
            'multiOptions' => array('Mr' => 'Mr','Ms' => 'Ms','Miss'
            => 'Miss','Mrs' => 'Mrs'),
        ));

//generated output
test

 Mr

Ms
Miss
Mrs

: 'disablefor' is not valid (X)HTML attribute in any case.

Proposed fix:

<

pre class="literal">

Index: FormLabel.php

--- FormLabel.php (revision 19018) +++ FormLabel.php (working copy) @@ -54,9 +54,13 @@ }

     $value = ($escape) ? $this->view->escape($value) : $value;

- $for = (empty($attribs['disableFor']) || !$attribs['disableFor']) - ? ' for="' . $this->view->escape($id) . '"' - : ''; + if (empty($attribs['disableFor']) || !$attribs['disableFor']) { + $for = ' for="' . $this->view->escape($id) . '"'; + } + else { + $for = ''; + unset($attribs['disableFor']); //Remove $attribs['disableFor'] from array + }

     // enabled; display label
     $xhtml = '<label'

Comments

What is the outcome you had expected, and how do you propose to solve this (without losing functionality, while generating valid html for other versions)?

The expected outcome is just

test, instead of test

as attribute 'disabledfor' is not allowed in any of HTML (and XHTML) flavors.

I think the expected behaviour was meant to be like commented by Mr O'Phinney:

http://framework.zend.com/issues/browse/…

I found problem in Zend_View_Helper_FormLabel.

If 'disableFor' attribute is set (and for radio it is) $for is cleaned to empty string, but disableFor attribute still exists $attribs array, so $this->_htmlAttribs($attribs) produces all attribs for label including disablefor="1". The simple solution could be just to remove array element:

// $for = (empty($attribs['disableFor']) || !$attribs['disableFor']) // ? ' for="' . $this->view->escape($id) . '"' // : '';

    if (empty($attribs['disableFor']) || !$attribs['disableFor']) {
        $for = ' for="' . $this->view->escape($id) . '"';
    }
    else {
        $for = '';
        //Remove $attribs['disableFor'] from array
        unset($attribs['disableFor']);
    }

if (empty($attribs['disableFor']) || !$attribs['disableFor']) { $for = ' for="' . $this->view->escape($id) . '"'; } else { $for = ''; unset($attribs['disableFor']); }

I found problem in Zend_View_Helper_FormLabel.

If 'disableFor' attribute is set (and for radio it is) $for is cleaned to empty string, but disableFor attribute still exists $attribs array, so $this->_htmlAttribs($attribs) produces all attribs for label including disablefor="1". The simple solution could be just to remove array element:

// $for = (empty($attribs['disableFor']) || !$attribs['disableFor']) // ? ' for="' . $this->view->escape($id) . '"' // : '';

    if (empty($attribs['disableFor']) || !$attribs['disableFor']) {
        $for = ' for="' . $this->view->escape($id) . '"';
    }
    else {
        $for = '';
        //Remove $attribs['disableFor'] from array
        unset($attribs['disableFor']);
    }

Sorry for double post.

Anyone can confirm this ?

What shall I do now ?

Added code to unset 'disableFor' attribute if set, attached the patch and test. Please review.

Changed diffs to svn-generated diffs instead of Netbeans.

Accidentally assigned to myself.

Patches applied to trunk and 1.9 release branch -- thanks!