html - How to write a custom form helper template for dynamically generated content? -
i have sort of quiz system, user gets shown question , several answer-options radio-buttons.
but using helper inputradiogroup gets filled via list, not pretty anymore (like twitter bootstrap). radiobuttons inline, while should underneath each other. , change icon prettier button.
this how looks @ moment:
therefore tried write own custom form helper, keep getting stuck. find frustratingly hard understand documentation this: https://www.playframework.com/documentation/2.3.x/javaformhelpers
first created new template named myfieldconstructortemplate.scala.html
@(elements: helper.fieldelements) <div class="@if(elements.haserrors) {error}"> <label for="@elements.id">@elements.label</label> <div class="input"> @elements.input <span class="errors">@elements.errors.mkstring(", ")</span> <span class="help">@elements.infos.mkstring(", ")</span> </div> </div>
saved /views-folder. try use in view class quiz.scala.html:
@import helper._ @import helper.twitterbootstrap._ @(questionlist: list[question], answerlist: list[answer], answerradioform: form[answer]) @helper.form(action = routes.application.nextquizpage(), 'id -> "answerradioform"){ @helper.inputradiogroup( answerradioform("answer"), options = answerlist.map(answer => answer.answerid.tostring -> answer.answertext), '_label -> "answer", '_error -> answerradioform("answerid").error.map(_.withmessage("select answer"))) <button type="submit" class="btn btn-default" value="send"> next question </button> } @implicitfield = @{ fieldconstructor(myfieldconstructortemplate.f) } @inputtext(answerradioform("questionid"))
if put template, not found: value implicitfield
-error.
so how can manage change appearance of radiobuttons underneath , looking twitter bootstrap?
[edit1]: have changed order of imports suggested version:
@(questionlist: list[question], answerlist: list[answer], answerradioform: form[answer]) @import helper._ @implicitfield = @{ fieldconstructor(myfieldconstructortemplate.f) } @import helper.twitterbootstrap._
i error then:
ambiguous implicit values: both method implicitfield of type => views.html.helper.fieldconstructor , value twitterbootstrapfield in package twitterbootstrap of type => views.html.helper.fieldconstructor match expected type views.html.helper.fieldconstructor
i think has way import answers radiobuttons?
[edit2]: order of imports now:
@(questionlist: list[question], answerlist: list[answer], answerradioform: form[answer]) @import models.question @import models.answer @import helper._ @implicitfield = @{ fieldconstructor(myfieldconstructortemplate.f) }
with this, program compiles. radiobuttons still same. tried change design, not quite work. looks radiobuttons melted single one:
here template class myfieldconstructortemplate.scala.html:
@(elements: helper.fieldelements) <div class="btn-group", data-toggle="buttons"> <label for="@elements.id">@elements.label</label> <div class="input"> <label class="btn btn-primary"> @elements.input <span class="errors">@elements.errors.mkstring(", ")</span> <span class="help">@elements.infos.mkstring(", ")</span> </label> </div> </div>
[edit3]: have changed class according to last answer, still radiobuttons melted each other. want point out not fixated on using inputradiogroup playframework helper, if there solution works same , looks bootstrap, gladly use that. seems changing helper isnt easy / intuitive. appreciate form of help!
the twitter bootstrap structure needs accurate.
wrap btn-group
class around inputradiogroup
helper so:
<div class="btn-group" data-toggle="buttons"> @helper.inputradiogroup( answerradioform("answer"), options = answerlist.map(answer => answer.answerid.tostring -> answer.answertext), '_label -> "answer", '_error -> answerradioform("answerid").error.map(_.withmessage("select answer"))) </div>
then replace template with:
@(elements: helper.fieldelements) <label class="btn btn-primary"> @elements.input @elements.label </label> <span class="errors">@elements.errors.mkstring(", ")</span> <span class="help">@elements.infos.mkstring(", ")</span>
in general, perhaps it'd of interest way of doing it. when use fooform("myfield"...)
, can use fooform("myfield[@i]"...)
in for
loop @i
counter going 0 many inputs there are. can sketch out full html instead of using implicit values.
by way, documentation scala version has lots more information java version. see here. has more information on inputradiogroup
java version of documentation still useful reading better understanding of this.
some play bootstrap plugin has code available on github useful reading uses implicit values also.
update
here's couple of github projects showing how achieve this:
- with scala version of play: https://github.com/bjfletcher/play-bootstrap-radio-group
- with java version of play: https://github.com/bjfletcher/play-java-bootstrap-radio-group
screenshot of result:
Comments
Post a Comment