How to enable displaying the global label unsing FormMultiCheckbox in ZF2?

48 Views Asked by At

I'm using Zend\Form\Element\MultiCheckbox with Zend\Form\View\Helper\FormMultiCheckbox:

MyFieldset.php

// namespace ...;
// use ....;
class MyFieldset extends Fieldset
{
    // ...
    public function init()
    {
        parent::init();
        $this->add(
            [
                'type' => 'multi_checkbox',
                'name' => 'mymulticheckbox',
                'options' => [
                    'label' => _('global label'),
                    'label_attributes' => [
                        'class' => 'col-md-3',
                    ],
                    'value_options' => [
                        [
                            'value' => 'foo',
                            'label' => 'FOO',
                        ],
                        [
                            'value' => 'bar',
                            'label' => 'BAR',
                        ],
                        [
                            'value' => 'buz',
                            'label' => 'BUZ',
                        ],
                    ]
                ],
            ]
        );
    }
    // ...
}

myform.phml

use Zend\Form\View\Helper\FormMultiCheckbox;
echo $this->formMultiCheckbox($myFieldset->get('mymulticheckbox'), FormMultiCheckbox::LABEL_PREPEND);

It works, but the "global label" is not displayed. It gets displayed, when I'm using Zend\Form\View\Helper\FormElement, but the FormMultiCheckbox seems to ignore the "global label".

How to make FormMultiCheckbox display the label of the checkbox list?

1

There are 1 best solutions below

2
On

Have you tried with formRow(). For me it works. This does not appear to be managed in formMultiCheckbox(). See lines 182-193, file zend-form/src/View/Helper/FormRow.php.

// Multicheckbox elements have to be handled differently as the HTML standard does not allow nested
// labels. The semantic way is to group them inside a fieldset
if ($type === 'multi_checkbox'
    || $type === 'radio'
    || $element instanceof MonthSelect
    || $element instanceof Captcha
) {
    $markup = sprintf(
        '<fieldset><legend>%s</legend>%s</fieldset>',
        $label,
        $elementString
    );