简体   繁体   中英

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

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?

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
    );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM