2012-04-18 63 views
2

使用這個文檔,我可以設置自己的幫手來佈局我的領域,但是我也想個性化一些場地。是否可以重寫表單助手?

的主要原因是Twitter的引導2,在這裏我需要改變(在checkbox.scala.html)

@input(field, args:_*) { (id, name, value, htmlArgs) => 
    <input type="checkbox" id="@id" name="@name" value="@boxValue" @(if(value == Some(boxValue)) "checked" else "") @toHtmlArgs(htmlArgs.filterKeys(_ == 'value))> 
    <span>@args.toMap.get('_text)</span> 
} 

到:

<label class="checkbox"> 
    <input type="checkbox" name="@name" id="@id" value="@boxValue" @(if(value == Some(boxValue)) "checked" else "") @toHtmlArgs(htmlArgs.filterKeys(_ == 'value)) /> 
    @args.toMap.get('_text) 
</label> 

我怎麼能這樣做? 感謝您的幫助!

回答

8

我終於做到了這樣:

我創建了一個包views.helpers.form,包含:

bootstrap.scala.html:

@(elements: helper.FieldElements) 

<div class="[email protected](elements.hasErrors) { error}"> 
    <label class="control-label" for="@elements.id">@elements.label(elements.lang)</label> 
    <div class="controls"> 
     @elements.input 
     @elements.infos(elements.lang).map { info => 
      <span class="help-inline">@info</span> 
     } 
     @elements.errors(elements.lang).map { error => 
      <span class="help-block">@error</span> 
     } 
    </div> 

checkbox.scala.html :

@** 
* Generate an HTML input checkbox. 
* 
* Example: 
* {{{ 
* @checkbox(field = myForm("done")) 
* }}} 
* 
* @param field The form field. 
* @param args Set of extra HTML attributes ('''id''' and '''label''' are 2 special arguments). 
* @param handler The field constructor. 
*@ 
@(field: play.api.data.Field, args: (Symbol,Any)*)(implicit handler: helper.FieldConstructor, lang: play.api.i18n.Lang) 

@boxValue = @{ args.toMap.get('value).getOrElse("true") } 

@helper.input(field, args:_*) { (id, name, value, htmlArgs) => 
    <label class="checkbox"> 
     <input type="checkbox" id="@id" name="@name" value="@boxValue" @(if(value == Some(boxValue)) "checked" else "") @toHtmlArgs(htmlArgs.filterKeys(_ == 'value))> 
     @args.toMap.get('_text) 
    </label> 


div> 
</div> 

而在我的模板中,我所要做的就是:

@import helper.{FieldConstructor, inputText, inputPassword} @** Import the original helpers *@ 
@import helpers.form.checkbox @** Import my helpers *@ 
@implicitField = @{ FieldConstructor(helpers.form.bootstrap.f) } 

而且瞧!有用!

+0

它有效。謝謝。 – masterdany88 2015-06-19 11:39:12

5

只用自己想要的代碼編寫自己的代碼並使用它代替提供的幫助程序會更簡單。它將簡化與覆蓋平臺標籤有關的潛在問題。

+1

是的,你是對的,這是一個好方法。我會試試看!謝謝! – 2012-04-18 11:05:57

相關問題