we had a queryStringBindable that we needed to use and had a similar situation, we found this question which gave us a clue but the answer from colinjwebb is out of date.
Here's our example which goes from a string to an Option[LoginContext].
package controllers
import play.api.mvc.{Action, AnyContent, QueryStringBindable, Request}
...
object BindableLoginContext {
implicit def queryStringBindable(implicit stringBinder: QueryStringBindable[String]) = new QueryStringBindable[LoginContext] {
override def bind(key: String, params: Map[String, Seq[String]]): Option[Either[String, LoginContext]] =
for {
loginContextString <- stringBinder.bind(key, params)
} yield {
loginContextString match {
case Right(value) if value.toLowerCase == "web" => Right(LoginContexts.Web)
case Right(value) if value.toLowerCase == "api" => Right(LoginContexts.Api)
case _ => Left(s"Unable to bind a loginContext from $key")
}
}
override def unbind(key: String, loginContext: LoginContext): String = stringBinder.unbind(key, loginContext.toString)
}
}
To use it we needed to use the following import:
import play.sbt.routes.RoutesKeys
and then add the object to the Project like this
lazy val microservice = Project(appName, file("."))
.settings(
RoutesKeys.routesImport += "controllers.BindableLoginContext._"
)