Pergunta

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?

Foi útil?

Solução

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 )
}

Outras dicas

Like @virtualeyes said, from the client side, DatePicker will generate the correct data format (dd/MM/yyyy by default).

However, Play Framework then needs to unmarshall the Date format correctly using bindFronRequest (client -> server).

Also Play needs to generate the correct Date string representation when generating the Form, which will be sent to the view (controller -> view). In java this can be done providing a DataBinder.

An example of this can be found in that issue opened on GitHub

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top