Php 如何在Zend framework-2表单中自定义复选框组?

Php 如何在Zend framework-2表单中自定义复选框组?,php,html,zend-framework,zend-framework2,zend-form,Php,Html,Zend Framework,Zend Framework2,Zend Form,实际上我不明白zend framework 2是如何为表单元素生成HTML的。比如说, $others = new Element\MultiCheckbox('others'); $others->setLabel('Others') ->setAttribute('name', 'others'); $others->setValueOptions(array( '1' => 'Contacts', '2' => 'Who vie

实际上我不明白zend framework 2是如何为表单元素生成HTML的。比如说,

$others = new Element\MultiCheckbox('others');
$others->setLabel('Others')
        ->setAttribute('name', 'others');
$others->setValueOptions(array(
    '1' => 'Contacts',
    '2' => 'Who viewd my profile'
));
此代码生成-

<label><input type="checkbox" value="1" name="others[]">Contacts</label>
<label><input type="checkbox" value="2" name="others[]">Who viewd my profile</label>
联系人
谁查看了我的个人资料
但是我需要按照如下方式制作HTML-

<input type="checkbox" value="1" name="others[]"><label>Contacts</label>
<input type="checkbox" value="2" name="others[]"><label>Who viewd my profile</label>
联系人
谁查看了我的个人资料

因此,如果我想更改生成的HTML,我该如何做呢?

对于这种功能,您需要覆盖它,或者更准确地说是覆盖它的功能

然后让
ViewHelperManager
知道
$this->formMultiCheckbox()
应该调用自己的
ViewHelper
来获得所需的结果

但是我想说的是,你所做的是非常令人沮丧的。用户完全可以点击标签!如果要更改标记,至少要这样做:

<input type="checkbox" value="1" name="others[]" id="cbOthers1"><label for="cbOthers2">Foo</label>
<input type="checkbox" value="2" name="others[]" id="cbOthers1"><label for="cbOthers2">Bar</label>

这可能会帮助你,我已经检查过了,但还没有得到复选框组的解决方案。我可以自定义一些,但不能用复选框组。当我写echo$this->formElement($elements['others'])时;它总是生成这些代码组@Amir:No也就是说,setlabel(“其他”)将为整个复选框组设置标签,默认情况下,每个标签内生成的复选框组的每个复选框都将设置标签。这就是问题所在。实际上,你提到的两种格式的结果都是一样的。你有什么不同,真的?
  switch ($labelPosition) {
     case self::LABEL_PREPEND:
        $template  = $labelOpen . '%s'. $labelClose .'%s';
        $markup    = sprintf($template, $label, $input);
     break;
     case self::LABEL_APPEND:
     default:
        $template  = '%s' . $labelOpen . '%s'. $labelClose;
        $markup    = sprintf($template, $input, $label);
      break;
 }