71143

Play 2 - Scala - Forms Validators and radio buttons

Question:

I'm need render a Form 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 fails on every <em>of[Char]</em> saying that its needed import play.api.data.format.Formats._ but I was.. My second doubt is about how to put a radio button pair for each (typo and isactive) thinking that typo has "M" and "A" like options and isactive has "Y" and "N".

PD: I think put this using a persistence model after...

Answer1:

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

To solve the problem you have two options:

<ol><li>Change the type from Char to String for which a default Formatter exists</li> <li>Supply a Formatter for Char</li> </ol>

A formatter would look something like this (note that it does not have the correct 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) }

Recommend

  • Android: openFileOutput throws NullPointerException
  • Starting a Thin cluster on Windows
  • Is DateTimeToString in Delphi XE5 doesn't work?
  • Understanding HttpServletRequest and cookies in JSF
  • Moving Circle on Live Wallpaper
  • Xamarin.Forms: How To Populate A Pie Chart From Web API Data?
  • How I can specify how rainbow color scheme should be converted to grayscale
  • Django non-rel createsuperuser fails due to non-ascii characters
  • How to assert page/tab/window title in Behat + Mink
  • How can I find the difference between two times [duplicate]
  • how to add Image in JPanel
  • How to highlighting active menu item on scroll and click?
  • ROR + MVC Disable Browser Cache
  • Preparing an Oracle Connection after being retrieved from a ConnectionPool
  • Calling a flash ExternalInterface in swiffyobject
  • How to debug iBeacons and Passbook
  • yii rewrite url with many sub categories
  • characters not allowed in DOM ids by spec, and by browser
  • Populating a database table with returned JSON
  • I need a PHP mySQL Login script that directs user to specific URL based on username and password
  • Get name of days between two date in ios?
  • content must have a ListView whose id attribute is 'android.R.id.list'
  • playing mp3 from nsbundle
  • How to make Rss News Reader application in android …? [closed]
  • Set SelectedIndex of ListView in FlipView_SelectionChanged event
  • Reload Page with Javascript after Database changes
  • ASP.NET MVC 2 actions for the same route?
  • How can i move Clearcase dyamic/snapshot views to another host (Linux)
  • Multiplying polynomials/simplifying like terms
  • How to define something in JavaScript [closed]
  • Amazon Elastick BeanStalk error: Failed to create the AWS Elastic Beanstalk application version
  • How to change user identity when git pushing via ssh?
  • How to decleare char *const argv[] in swift [duplicate]
  • read part of h5 dataset python
  • Bitrate JWplayer
  • Grails - How to implement a foreign key relationship not using an id column?
  • Codeigniniter insert data through models and controller
  • Write to .csv file with PHP (Commas in Data Error)
  • convert json to excel in java
  • ARKit code issue {unknown error -1=ffffffffffffffff error: Task failed with exit 1}