我在这里使用 HTML::FormHanlder。 我试图通过渲染单选按钮( using this method )获得不同的输出。字段声明如下:
has_field 'xxx' => ( type => 'Select', widget => 'RadioGroup', build_label_method => \&build_label );
sub build_label {
my $self = shift;
return $self->name;
}
问题是,唯一的<label>
位于元素分组标题中:
<label for="xxx">Lorem ipsum</label>
,
所以它改变了这一点。
单选按钮保持不变,如 <input type="radio" name="xxx" id="xxx" value="2"/>
I'm not changed
很自然地,我想知道如何更改自动呈现的“我没有改变”(在本例中)<input/>
之后的文本。
下面是一个更清楚的示例:
<label for="0.xxx">This is the only part that gets changed with sub build_label</label>
<label class="radio" for="0.xxx.0">
<input type="radio" name="0.xxx" id="0.xxx.0" value="2"/>
How to change rendering method of this part?
</label>
<label class="radio" for="0.xxx.1">
<input type="radio" name="0.xxx" id="0.xxx.1" value="1"/>
And this one?
</label>
最佳答案
解决方案取决于您想要更改单选组选项标签的原因。如果您查看 HTML::FormHandler::Widget::Field::RadioGroup 中的代码,您可以了解该字段是如何呈现的。
通常,您会使用所需的标签构建选项列表。您可以在该字段上提供 options_method:
has_field 'xxx' => ( type => 'Select', widget => 'RadioGroup', options_method => \&build_xxx_options );
sub build_xxx_options {
my $self = shift; # $self is the field
<build and return options with desired labels>;
}
如果您想本地化标签,只要您为 maketext 提供合适的翻译文件,就会自动进行。即使您不想本地化字符串,您也可以利用标签已本地化的事实(我的 $label = $self->_localize($option_label);) 并为该字段提供本地化方法,方法是将“localize_meth”设置为方法引用:
has_field 'xxx' => ( type => 'Select', widget => 'RadioGroup', localize_meth => \&fix_label );
sub fix_label {
my ( $self, $label ) = @_; # $self is the field
if ( $label eq '...' ) {
return '....';
}
return $label;
}
关于perl - 渲染RadioGroup元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14020267/