0

我使用Zend\Form\Element\MultiCheckboxZend\Form\View\Helper\FormMultiCheckbox如何启用在ZF2中显示全局标签unsof 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); 

它的工作原理,但不显示 “global label”。它被显示,当我使用Zend\Form\View\Helper\FormElement时,但FormMultiCheckbox似乎忽略了“全球label”。

如何使FormMultiCheckbox显示复选框列表的label

回答

0

您是否尝试过formRow()。对我来说它有效。这看起来没有在formMultiCheckbox()中进行管理。见线182-193,文件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 
    ); 
+0

谢谢你的回答!是的,正如我在我的问题中写的,我已经用'FormRow'视图助手来试用它。不幸的是,它有缺点,那么标签位置不能被定义为希望('formRow($ field,'prepend')'不适用于'MultiCheckbox'es)。最后,我通过手动完成所有工作来完成工作:''

'。 $ this-> translate($ myFieldSet-> get('mymulticheckbox') - > getLabel())。 ''。 $ this-> formMultiCheckbox($ myFieldSet-> get('mymulticheckbox'),FormMultiCheckbox :: LABEL_PREPEND)'这很丑,但它的工作原理。 – automatix

+0

哦,是的,没有其他的选择。 –

相关问题