Play 2 - Scala - Form Validators and Switches

I need a form visualization with validators for this model:

Model:

case class Service ( name: String, description: String, unitcost: Long, typo: Char, isactive: Char, modifiedby: String) 

Controller:

  import play.api.data.Form import play.api.data._ import play.api.data.format.Formats._ import play.api.data.Forms._ object Services extends 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) ) 

This code does not work on each of [Char], saying that its necessary import is play.api.data.format.Formats._ but I was ..

My second doubt is how to put a couple of radio buttons for each (typo and inactive) thinking that typos have "M" and "A", such as options and inactive, have "Y" and "N".

PD: I think put it using the save model after ...

+4
source share
1 answer

The error indicates that the form does not know how to handle the Char type. There is no default value for the Char type.

To solve the problem, you have two options:

  • Change the type from Char to String , for which there is a default Formatter
  • Put a Formatter for Char

The formatting element will look something like this (note that it does not have proper error handling)

 implicit val charFormat = new Formatter[Char] { def bind(key: String, data: Map[String, String]):Either[Seq[FormError], Char] = data.get(key) .filter(_.length == 1) .map(_.head) .toRight(Seq(FormError(key, "error.required", Nil))) def unbind(key: String, value: Char) = Map(key -> value.toString) } 
0
source

Source: https://habr.com/ru/post/1441876/


All Articles