Scala – Play Framework 2.1 Scala – form binding for date

formsplayframework-2.0playframework-2.1scala

I want to create a form binding in Play Framework 2.1, for a form that has date/time fields. Is there any standard verifier for date/time input? I understand that the page form should also send the date/time in a particular format. Does anyone know of any prefab solution for that? Or can describe how to implement one by myself?

Best Answer

Play 2.1 has built-in support for Twitter Bootstrap; if you take that route then Bootstrap Date Picker is a good call for client-side (i.e. ensuring date is sent as yyyy-mm-dd or other valid date format).

With the client taken care of, server-side Play 2.1 supports JodaTime, so you can bind the post'd form date like so:

object FooForm {
  import play.api.data.{Form, Forms}, Forms._
  val mapper = mapping(
    'fooDate-> jodaDate("yyyy-MM-dd")
  )(Foo.apply)(Foo.unapply)
  val form = Form( mapper )
}
Related Topic