Multiple FieldConstructor in PlayFramework 2.1?

playframework, playframework-2.0, playframework-2.1

Solution

Short version:

@(consultForm: Form[Consult])(implicit lang: play.api.i18n.Lang)

@import views.html.helper.FieldConstructor
@implicitFieldConstructor = @{ FieldConstructor(twitterBootstrapInput.f) }

@* uses twitterBootstrapInput */
@helper.inputText(consultForm("name"))

@* uses anotherFieldConstructor */
@helper.inputRadioGroup(consultForm("currency") 
  , options = Seq(
    "USD" -> "USD" 
  , "HKD" -> "HKD" 
  , "RMB" -> "RMB") 
  , '_label -> "Currency" 
  , '_error -> consultForm("currency").error.map(_.withMessage("select currency"))
)(FieldConstructor(anotherFieldConstructor.f), lang)


@* uses twitterBootstrapInput */
@helper.inputText(consultForm("anotherFormFieldName"))

`anotherFieldConstructor` is an additional FieldConstructor you have to create. It should place the radio buttons according your needs.

Long version:

The API DOC for inputRadioGroup is here: http://www.playframework.com/documentation/api/2.1.0/scala/index.html#views.html.helper.inputRadioGroup$

Since inputRadioGroup is a Scala singleton object the following code statements are the same:

helper.inputRadioGroup(consultForm("currency") /* etc. */)
helper.inputRadioGroup.apply(consultForm("currency") /* etc. */)

The apply method of inputRadioGroup has two parameter lists. The second list uses implicit parameters.

With `@implicitFieldConstructor = @{ FieldConstructor(twitterBootstrapInput.f) }` you specified, that the `twitterBootstrapInput` FieldConstructor is the "default parameter" for handler in the second parameter list of inputRadioGroup.

But you can overwrite it with explicit parameters:

helper.inputRadioGroup(consultForm("currency") /* etc. */)(FieldConstructor(anotherFieldConstructor.f), lang)

If you use Scala controllers the template needs an additional parameter list for lang:

@(consultForm: Form[Consult)])(implicit lang: play.api.i18n.Lang)

You can't have two implict FieldConstructors in a template.

Problem

I am trying to build a custom FieldConstructor in the scala doc. I follow the instruction , build a `twitterBootstrapInput.scala.html` , and it works ... This is my output : Here comes the problem : I want the `@helper.inputRadioGroup` spans "horizontally" , not vertically. (Because `twitterBootstrapInput.scala.html` warps `@elements.input` in `<div>` block ) But I don't know how to modify the template without infecting other "textfields" ? Should I define another implicitFieldConstructor ? Or do something else ? I cannot find an example about how to solve this problem ... All the custom template documents are too rare... Can somebody give me an example ? Thanks ! This is my code (play 2.1) : ``` @import views.html.helper.FieldConstructor @implicitFieldConstructor = @{ FieldConstructor(twitterBootstrapInput.f) } @helper.inputRadioGroup(consultForm("currency") , options = Seq( "USD" -> "USD" , "HKD" -> "HKD" , "RMB" -> "RMB") , '_label -> "Currency" , '_error -> consultForm("currency").error.map(_.withMessage("select currency")) ) ``` ======== April/11 updated ============== Thanks @Schleichardt for giving me a first step. I append `(FieldConstructor(twitterBootstrapRadioGroup.f) , lang)` after the `inputRadioGroup` , it seems working. But , even I write the simplest `@elements.input` in the template (no other decorations) , it still spans vertically. As the following screenshot : And the output html source code is : How to modify the `<span class="buttonset" ...>` block ? Shouldn't I call `@elements.input` in the template ?

Original source