playframework/Play20 — pull request #381 by natalinobusa (2012-07-09 21:22:45+00:00)
Title: Added a default value feature on Forms

I would like to propose this default feature to reduce the amount of user code required to define default values (programmatically and not via the routes)

We are writing a web service in scala/akka/play and play seems to be currently more tailored towards easy of use (see the good work on routes) but less on request programming flexibility when it comes to complex action parameters binding. Therefore the proposed "default" addition to the set of Form binding modifiers.

For default-free routes:

Currently:

def test = Action {
implicit request =>

case class CData(top:Int)

val p = Form(
mapping(
"top" -> optional(number)
)((top) => CData(top.getOrElse(42))) ((cdata:CData) => Some(Some(cdata.top)))
).bindFromRequest()

Ok("all done. " + p.get.toString)
}

with the new feature

def test = Action {
implicit request =>

case class CData(top:Int)

val p = Form(
mapping(
"top" -> default(number, 2)
) (CData.apply) (Cdata.unapply)
).bindFromRequest()

Ok("all done. " + p.get.toString)
}