forms - Scala example of inputRadioGroup in Play2.1 -
i'm browsing around in documentation can't find example of how use inputradiogroup in controller.
i guess should use this helper. how bind form in controller? show radio group represents grades 1 - 5
controller:
object sms extends controller { val testform: form[test] = form ( mapping( "firstname" -> nonemptytext, "lastname" -> nonemptytext, "password" -> tuple( "main" -> text(minlength = 6), "confirm" -> text ).verifying( "passwords don't match", passwords => passwords._1 == passwords._2 ), "email" -> tuple( "main" -> (text verifying pattern("^([0-9a-za-z]([-\\.\\w]*[0-9a-za-z])*@([0-9a-za-z][-\\w]*[0-9a-za-z]\\.)+[a-za-z]{2,9})$".r, error="a valid email req")), "confirm" -> text ).verifying( "emails don't match", emails => emails._1 == emails._2 ), "grade" -> magic happen here? )(test.apply)(test.unapply) ) case class test( firstname: string, lastname: string, password: string, email: string, grade: int ) } view:
@inputradiogroup( testform("grade"), options = seq("1"->1,"2"->2....and on) '_label -> "grade", '_error -> testform("grade").error.map(_.withmessage("some error"))) i can't figure out how this.
in controller create seq of possible grades , pass seq view. using case class grade better pass tuple2[string, string] view. guess that's matter of opinion.
case class grade(value: int, name: string) private val grades = seq(grade(1, "brilliant"), grade(2, "good"), grade(3, "ok")) val testform: form[test] = form (... "grade"-> number )(test.apply)(test.unapply) def edit(id: long) = action { val model = ...obtain model ok(views.html.edit(testform.fill(model), grades)) } def submit() = action { testform.bindfromrequest.fold( formwitherrors => ok(views.html.edit(formwitherrors, grades)) }, test => { logger.info("grade: " + grades.find(_.value == test.grade).map(_.name)) //save model... redirect(... }) } in view, map grades seq tuple2[string, string] feed inputradiogroup
@(testform: form[test], grades: seq[grade]) @inputradiogroup(contactform("grade"), options = grades.map(g => g.value.tostring -> g.name), '_label -> "grade")
Comments
Post a Comment