实际上我不能理解,zend框架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,我该怎么做?
对于这种特性,您需要覆盖Zend'Form'View'Helper'MultiCheckbox
,或者更准确地说,覆盖它的renderOptions()
功能。
然后让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>
永远不要忘记应用程序的可用性!另一个提示:就浏览器对样式的支持而言,在标签中自动添加CB可以使您获得更广泛的受众!不过话说回来,一切都取决于你。无论如何,你必须自己编写ViewHelper
。
PS:您的ViewHelper
将非常容易,您只需要将这些行覆盖为以下内容:
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;
}