我在这里使用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>
答案 0 :(得分:2)
解决方案取决于您要更改单选按钮组选项的标签的原因。如果您查看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-&gt; _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;
}