Frage

Ich versuche, eine Fallklasse auf ein einzelnes Feld innerhalb eines Scala Play-Formulars anzuwenden.Das Feld, auf das ich es anwenden möchte, ist exerciseName innerhalb der setsForm Variable.

  case class ExerciseName(exerciseName:String)
  case class WorkoutSet(exerciseName:ExerciseName, number:Int)
  case class WorkoutSets(sets:List[WorkoutSet]) 
  val setsForm:Form[WorkoutSets] = Form( 
    mapping( 
      "workoutSets" -> list(mapping
      (   
        //i need to get exerciseName to be of type ExerciseName somehow...
        "exerciseName" ->nonEmptyText,  
        "workoutSet" -> number(min=1,max=20)
        )(WorkoutSet.apply)(WorkoutSet.unapply))
    )(WorkoutSets.apply)(WorkoutSets.unapply)
  )

Ich habe mich gefragt, ob mir jemand hierzu einen Einblick geben könnte.

Danke!

War es hilfreich?

Lösung

val setsForm:Form[WorkoutSets] = Form(
    mapping(
      "sets" -> list(
        mapping(
          "exerciseName" -> mapping("exerciseName" -> nonEmptyText)(ExerciseName.apply)    (ExerciseName.unapply),
          "workoutSet" -> number(min=1,max=20)
        )(WorkoutSet.apply)(WorkoutSet.unapply)
      )
    )(WorkoutSets.apply)(WorkoutSets.unapply)
)

Ich würde außerdem empfehlen, den Feldnamen von ExerciseName in „name“ zu ändern, um Verwirrung zu vermeiden.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top