2012-12-24 148 views
2

我在这里使用HTML :: FormHanlder。 我试图用渲染单选按钮获得不同的输出(using this method)。字段声明是这样的:渲染RadioGroup元素

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> 

回答

2

解决方案将取决于为什么要更改无线电组选项的标签。如果您查看HTML :: FormHandler :: Widget :: Field :: RadioGroup中的代码,则可以阅读该字段的呈现方式。

通常情况下,您将构建带有所需标签的选项列表。如果您要本地化的标签,如果你提供合适的翻译文件maketext会自动发生

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>; 
} 

:你可以提供有关领域的options_method。即使你不想本地化字符串,也可以利用标签本地化的事实(我的$ 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; 
} 
+1

感谢您花时间注册并回答问题,您让我走上正轨。不知何故'options_method'没有像预期的那样工作(我无法获得'$ self-> schema'等),但是'sub options_ '完成了这项工作。 P.S.我真的去寻找源代码,试图创建一个新的Moose :: Role,它具有'HTML :: FormHandler :: Widget :: Field :: RadioGroup'',所以我可以修改'wrap_radio'方法,但是我想我不能在那里使用DBIx :: Class,所以我不得不尝试其他的:) –

+1

'options_method'提供了一个字段方法,所以这个模式在$ self-> form-> schema中。 'options_ '方法是一种表单方法,因此该模式位于$ self> schema中。可以在RadioGroup和CheckboxGroup小部件中使用'label_method'回调,但由于可以使用正确的标签创建选项,因此我不清楚是否需要这种回调。 – gshank