ZF-2976: Zend_View_Helper_FormRadio have no unique ids for javascript access


As an inconstancy with other view helpers, formRadio doesn't give the radio buttons a unique id.

I came by this error while using Zend_Form, which depends on this View_Helper class

Zend_Form_Decorator_ViewHelper eventually calls the helper with something like seen below:

formRadio('subfrm', 'true', array('options' => array('true' => '', 'false' => ''), 'listsep' => ''));

This results in:

While the expected result would be something like:

A suggested fix would be:

// Wrap the radios in labels
            $radio = '_htmlAttribs($label_attribs) . '>'
                    . (('prepend' == $labelPlacement) ? $opt_label : '')
                    . '_htmlAttribs($attribs) 
                    . $endTag
                    . (('append' == $labelPlacement) ? $opt_label : '')
                    . '';

The preg replace is there to also address names with brackets. So subfrm[0] with value true would be converted to subfrm-0-true. It was copied from Zend_Form_Decorator_ViewHelper, so i think it's safe, but didn't create it.


Linked to ZF-2937, since that was the original filed bug report. However this bug should be categorized with Zend_View instead.

Please evaluate and categorize as necessary.

The suggested fix does not work on names like "mainProducts[]" (to submit arrays when using Zend_View_Helper_FormMultiCheckbox). It would generate an id like "mainProducts[]-product1", which is invalid according to W3C. I used this instead: \ \

                    . ' id="' . rtrim(preg_replace('/\[([^\]]*)\]/', '-$1', $name), '-') .'-'. $this->view->escape($opt_value) . '"' //add this line

Scheduling for next mini release. Fix is non trivial, so have scheduled a good time alotment for dealing with it.

Resolved in trunk and 1.5 and 1.6 release branches.

Using the option value to concatenate to element name as in the current implementation (1.6.1) may result in invalid option IDs, when using a non-ascii value (e.g. äöü as the option value results in ) I'd suggest using an option counter and add a option count number instead of the option value, which is just as useful, but avoids possible problems.

in FormRadio, instead of // generate ID $optId = $id . '-' . $filter->filter($opt_value);

this could be simply // generate ID $optNo ++; $optId = $id . '-' . $optNo; avoiding the need for an additional filter, which will always lead to unpredictable results.

At this point, changing the id name of a formradio element would produce a BC break. We will address this at 2.0 time.