Lilluda 5
Lilluda 5

Reputation: 1109

Scala Play Map Forms

This has been asked multiple times on this site, but I still can't figure out the answer. I am trying to map a form to a case class that I have, here is the case class:

  case class MapVitalSign(id:Long,name:String,
    lowerBoundComplicationId:Long, upperBoundComplicationId:Long,
    lowerBound:Double,upperBound:Double) extends VitalSign(  
    id,name,
    lowerBoundComplicationId,upperBoundComplicationId,
    lowerBound,upperBound)

and here this the code I am trying to map it to.

  val vitalSignForm: Form[MapVitalSign] = Form(
    mapping(
      "id" -> ignored(Long), 
      "name" -> text, 
      "lowerBoundComplicationId" -> number, 
      "upperBoundComplicationId" -> number,  
      "lowerBound" -> number, 
      "upperBound" -> number)
      ((id,name,lowerBoundComplicationId,
      upperBoundComplicationId,lowerBound,
      upperBound) => MapVitalSign(id,name,
      lowerBoundComplicationId.toLong,
      upperBoundComplicationId.toLong,lowerBound.toDouble,
      upperBound.toDouble),
      (v:MapVitalSign) => Some(v.id,v.name,v.lowerBoundComplicationId.toInt,v.upperBoundComplicationid.toInt, lowerBound.toInt,upperBound.toInt)    )   
  )

Where is my mapping error occurring, as far as I can tell everything seems to be mapping to the correct type, and the amount of arguements is correct. Is it something to do with the "id" field being ignored initially (as my posgres db hasn't generated one)?

Upvotes: 0

Views: 631

Answers (2)

OlivierBlanvillain
OlivierBlanvillain

Reputation: 7768

If your form and your case class have the same fields you should be able to use the case class apply and unapply methods:

import play.api.data.format.Formats._

val vitalSignForm: Form[MapVitalSign] = Form(
  mapping(
    "id" -> of[Long],
    "name" -> text, 
    ...
  )(MapVitalSign.apply _)(MapVitalSign.unapply _)
)

Upvotes: 1

kompot
kompot

Reputation: 798

As documentation for ignored method states "as we ignore this parameter in binding/unbinding we have to provide a default value" you should provide default value for it. So replacing ignored(Long) with ignored(0L) should help.

Upvotes: 0

Related Questions