> The Forms API uses implicit conversions to convert form values into
> whatever you need, and we haven't yet implemented one for converting to
> Char. However, you can implement your own, eg:
> implicit def charFormat: Formatter[Char] = new Formatter[Char] {
> override val format = Some(("format.char", Nil))
> def bind(key: String, data: Map[String, String]) =
> parsing(_.head, "error.char", Nil)(key, data)
> def unbind(key: String, value: Char) = Map(key -> value.toString)
> }
> Note that the error.char and format.char keys above you'll have to define
> in your messages file.
> Though, if you're expecting just values of Y or N, why not treat it as a
> Boolean? Then you could do something like this:
> val ynFormat = new Formatter[Boolean] {
> override val format = Some(("format.yn", Nil))
> def bind(key: String, data: Map[String, String]) = {
> Right(data.get(key).getOrElse("N")).right.flatMap {
> case "Y" => Right(true)
> case "N" => Right(false)
> case _ => Left(Seq(FormError(key, "error.yn", Nil)))
> }
> }
> def unbind(key: String, value: Boolean) = Map(key -> value match {
> case true -> "Y"
> case false -> "N"
> })
> }
> and in your form:
> typo -> of(ynFormat)
> isActive -> of (ynFormat)
> Even simpler would be rather than use Y and N, use true and false, and
> then just declare to be of[Boolean], and everything will just work.
> On Thursday, 25 October 2012 03:00:33 UTC+11, Christian Espinoza wrote:
>> Hello, I'm newbie with playand I'm try to render this Form:
>> Model:
>> case class Service (
>> name: String, description: String, unitcost: Long, typo: Char, isactive:
>> Char, modifiedby: String)
>> Controller:
>> private val servicesForm[Service] = Form(
>> mapping(
>> "name" -> nonEmptyText.verifying(
>> "validation.name.duplicate", Service.findByName(_).isEmpty),
>> "description" -> nonEmptyText,
>> "unitcost" -> longNumber,
>> "typo" -> of[Char],
>> "isactive" -> of[Char],
>> "modifiedby" -> nonEmptyText
>> ) (Service.apply)(Service.unapply)
>> )
>> But I get this Error: Cannot find Formatter type class for Char. Perhaps
>> you will need to import play.api.data.format.Formats._
>> Both vars are Char type that I need fill each one with a char value like
>> 'Y' or 'N', for this I want a radio button pair for each selection
>> but I don't know how to do that...
>> I can't find samples to guide me with this..
>> PD: I had imported play.api.data.format.Formats._
>> Thanks in Advance.
>> Christian.