2012-12-24 145 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