playframework/Play20 — pull request #379 by natalinobusa (2012-07-08 10:01:32+00:00)
Title: default value for optional fields

I would like to propose this change about the optional mapping to reduce the amount of user code required to define default values

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" -> optional(number, 2)
) (CData.apply) (Cdata.unapply)
).bindFromRequest()

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