ZF-9184: Zend_Form_Element_MultiCheckbox improvement for groups of checkboxes

Issue Type: Improvement Created: 2010-02-15T18:56:39.000+0000 Last Updated: 2012-11-20T21:38:13.000+0000 Status: Open Fix version(s): Reporter: Ziad Saab (ziadsaab) Assignee: None Tags: - Zend_Form

Related issues: Attachments:


When creating a Zend_Form_Element_Multiselect, if the multiOptions array is a nested associative array, then ZF will nicely create optgroups in the element. I have created a view helper that lets me do the same with groups of checkboxes, grouped inside fieldsets, with the


legend> element acting as the group name.

It seems to me that this is an intuitive and sensible way to group groups of checkboxes that have a common purpose. My use case was when designing a form that lists checkboxes of features. In my data set, the features are logically grouped by feature type. To go into more detail, the application is a home renting app. Some feature type examples are "general", "kitchen", "bedroom", etc. and examples of features are "accept credit cards" (under general) or "freezer" (under kitchen). The reason why it was designed like this is so that an administrator can add new feature types and features in the CMS, without having to create new and useless tables in the application's database. The form to manage houses is built dynamically, so by creating my view helper, I was able to only have the following code:

<pre class="highlight">
$el = new Zend_Form_Element_MultiCheckbox('features');
$el->helper = 'groupedMultiCheckbox';

And here is the code for the grouped multi checkbox view helper, building on top of the current formMultiCheckbox helper. I would like to have your feedback on this code, as I feel that a better general knowledge of Zend_Form would have enabled me to create a more generalized view helper for this use case. For example some people might have liked to use simple div tags instead of fieldset + legend, or maybe nested ul + li elements?

I hope this will be useful to someone and that it can be implemented in a more generalized way in a future ZF release.

<pre class="highlight">
class My_View_Helper_GroupedMultiCheckbox extends Zend_View_Helper_FormMultiCheckbox
     * Generates a set of checkbox button elements, grouped by 
     * @access public
     * @param string|array $name If a string, the element name.  If an
     * array, all other parameters are ignored, and the array elements
     * are extracted in place of added parameters.
     * @param mixed $value The checkbox value to mark as 'checked'.
     * @param array $options An array of key-value pairs where the array
     * key is the checkbox value, and the array value is the radio text.
     * @param array|string $attribs Attributes added to each radio.
     * @return string The radio buttons XHTML.
    public function groupedMultiCheckbox($name, $value = null, $attribs = null,
        $options = null, $listsep = "<br></br>\n")
        if (is_array($options))
            $html = array();
            $translator = $this->getTranslator();
            foreach ($options as $group => $values)
                $group_html = array();
                $group_html[] = '';
                $text_group = null !== $translator ? $translator->translate($group) : $group;
                $group_html[] = '' . $this->view->escape($text_group) . '';
                $group_html[] = $this->formMultiCheckbox($name, $value, $attribs, $values, $listsep);
                $group_html[] = '';

                $html[] = implode("\n", $group_html);
            return implode("\n", $html);
            return $this->formMultiCheckbox($name, $value, $attribs, $options, $listsep);


No comments to display

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.