Re: [2.0-scala] Form with a char type (Radio Button howto)

228 views
Skip to first unread message

James Roper

unread,
Oct 25, 2012, 8:08:04 PM10/25/12
to play-fr...@googlegroups.com
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.

Christian Espinoza

unread,
Oct 26, 2012, 11:24:15 AM10/26/12
to play-fr...@googlegroups.com
Thanks James, keeping on mind your tips, I'm thinking should be more easy to use these vars like Boolean types and on the model translate it to chars 
in order to adjust it to my Database schema...

Thanks.

PD: To work with datetime stamps from the database, then I should use it like a string and parse any format that I'll need??

Regards.
Christian.
Reply all
Reply to author
Forward
0 new messages