I would like to have java.sql.Date and Option[java.sql.Date] in my Play-scala project as a query-paramater, which don't come as a default with the Play framework. Play-version I'm using is 2.4.3. I have following (rough) class.
object CustomBinders extends {
val dateFormat = ISODateTimeFormat.date()
implicit def dateBinder: QueryStringBindable[Date] = new QueryStringBindable[Date] {
def bind(key: String, params: Map[String, Seq[String]]): Option[Either[String, Date]] = {
val dateString: Option[Seq[String]] = params.get(key)
try {
Some(Right(new Date(dateFormat.parseDateTime(dateString.get.head).getMillis)))
} catch {
case e: IllegalArgumentException => Option(Left(dateString.get.head))
}
}
def unbind(key: String, value: Date): String = {
dateFormat.print(value.getTime)
}
}
}
Then in Build.scala I have
import play.sbt.routes.RoutesKeys
object Build extends Build {
RoutesKeys.routesImport += "binders.CustomBinders.dateBinder"
RoutesKeys.routesImport += "binders.CustomBinders.optionDateBinder"
However if I define a query-parameter with Option[Date] for an example, I'm getting an error
No QueryString binder found for type Option[java.sql.Date]. Try to implement an implicit QueryStringBindable for this type.
So it obviously isn't the scope. How should I define the Binders so that they exist in the scope? I can't find the 2.4-documentation for this, but 2.5-documentation doesn't say anything about needing to add them to Build.scala
So appereantly the Build.scala wasn't the right place... Even though some documentations tell to put it there. When in build.sbt
routesImport += "binders.CustomBinders._"
The project compiles just fine. Fixed some faults in the original post for the Binder as well.