Added a default value feature on Forms #381

Merged
merged 2 commits into from Sep 4, 2012

Projects

None yet

3 participants

@natbusa
Contributor
natbusa commented Jul 9, 2012

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 ease of use and web apps/sites (see the good work on routes) but less on web apis and requests handled as part of the controller, especially when it comes to complex action parameters binding. The optional feature falls in my opinion a bit short, since it creates an "impedence mismatch" between the output of the binding and the typical case classes you would like to define. 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. ")
}

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. ")
}
@pk11
Collaborator
pk11 commented Jul 13, 2012

Could you please sign our CLA? http://www.typesafe.com/contribute/cla

@natbusa
Contributor
natbusa commented Jul 13, 2012

Hi Peter, the company I work for (http://www.civolution.com) is currently reviewing the CLA. I'll get back to you as soon as they have reached a decision.

@natbusa
Contributor
natbusa commented Jul 17, 2012

Just signed the CLA.

@natbusa
Contributor
natbusa commented Jul 26, 2012

Hi, any news on this pull request?

@opensas
Contributor
opensas commented Aug 10, 2012

nice contribution...

@pk11 pk11 merged commit 8e9e0a6 into playframework:master Sep 4, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment