实际上我不能理解,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,我该怎么做呢?
发布于 2013-06-03 13:44:27
对于这种特性,您需要重写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将会非常简单,你只需要将those lines改写成以下代码:
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;
}https://stackoverflow.com/questions/16890142
复制相似问题