Skip to content

Instantly share code, notes, and snippets.

@wolfendale
Created November 13, 2015 13:30
Show Gist options
  • Save wolfendale/1309f5a2d29e0bb9cfcc to your computer and use it in GitHub Desktop.
Save wolfendale/1309f5a2d29e0bb9cfcc to your computer and use it in GitHub Desktop.
Play Framework Boolean Formatter
implicit val booleanFormatter: Formatter[Boolean] = new Formatter[Boolean] {
override val format = Some(("format.boolean", Nil))
override def bind(key: String, data: Map[String, String]): Either[Seq[FormError], Boolean] =
data.get(key) match {
case Some(value) => value match {
case "true" => Right(true)
case "false" => Right(false)
case _ => Left(Seq(FormError(key, "error.boolean", Nil)))
}
case _ => Left(Seq(FormError(key, "error.boolean.empty", Nil)))
}
override def unbind(key: String, value: Boolean): Map[String, String] = Map(key -> value.toString)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment