[英]How to make custom Radio-Button labels in ZF2 forms?
I have a form with Radio-button: 我有一个带单选按钮的表单:
$this->add([
'name' => 'time',
'options' => [
'value_options' => [
'0' => '9:00 - 12:00',
'1' => '12:00 - 16:00',
'2' => '16:00 - 19:00',
],
'label_attributes' => [
'class' => 'WW_OBJ_fm-label',
]
],
'type' => 'Radio'
]);
In the view I make the output like this: 在视图中我输出如下:
<div>
<?php echo $this->formElement($form->get('time')); ?>
</div>
and get the output (formatted for readability): 并获取输出(格式化为可读性):
<div>
<label class="WW_OBJ_fm-label">
<input type="radio" name="time" value="0"/>
9:00 - 12:00
</label>
<label class="WW_OBJ_fm-label">
<input type="radio" name="time" value="1"/>
12:00 - 16:00
</label>
<label class="WW_OBJ_fm-label">
<input type="radio" name="time" value="2"/>
16:00 - 19:00
</label>
</div>
But I need, that label text ist wrapped by a <span>
: 但我需要,标签文本由<span>
包裹:
<div>
<label class="WW_OBJ_fm-label">
<input type="radio" name="time" value="0"/>
<span class="WW_label-text">9:00 - 12:00</span>
</label>
<label class="WW_OBJ_fm-label">
<input type="radio" name="time" value="1"/>
<span class="WW_label-text">12:00 - 16:00</span>
</label>
<label class="WW_OBJ_fm-label">
<input type="radio" name="time" value="2"/>
<span class="WW_label-text">16:00 - 19:00</span>
</label>
</div>
What is the best way to achieve it? 实现它的最佳方法是什么?
A solution is to use labelOption 'disable_html_escape' : 解决方案是使用labelOption'disable_html_escape':
$this->add([
'name' => 'time',
'options' => [
'value_options' => [
'0' => '<span class="WW_label-text">9:00 - 12:00</span>',
'1' => '<span class="WW_label-text">12:00 - 16:00</span>',
'2' => '<span class="WW_label-text">16:00 - 19:00</span>',
],
'label_attributes' => [
'class' => 'WW_OBJ_fm-label',
]
],
'type' => 'Radio'
]);
$element = $this->get('time');
$element->setLabelOptions(['disable_html_escape' => true]);
I see three possible solutions for your problem. 我看到了三种可能的解决方案。
1) Extend the Zend\\Form\\View\\Helper\\FormRadio
class, overriding the renderOptions
method, replicating almost entirely the one that you can find in Zend\\Form\\View\\Helper\\FormMultiCheckbox
but maybe adding an option to pass optional attributes to the span
element 1)扩展Zend\\Form\\View\\Helper\\FormRadio
类,重写renderOptions
方法,几乎完全复制你可以在Zend\\Form\\View\\Helper\\FormMultiCheckbox
但可能添加一个选项来将可选属性传递给span
元素
2) Very subtle, but could save you writing some code: using the translator. 2)非常微妙,但可以节省你写一些代码:使用翻译。 Since the radio value options are translated, you could keep your values defined in the configuration but adding the span
element in the transation 由于无线电值选项已转换,您可以保留在配置中定义的值,但在转换中添加span
元素
3) Do not use $this->formElement
to display the element, but actually write all the html 3)不要使用$this->formElement
来显示元素,而是实际写出所有的html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.