Applying case class to indidual field in a nested form Scala Play
-
20-12-2019 - |
Question
I am trying to apply a case class to an individual field inside of a Scala Play form. the field I am trying it apply it to is exerciseName
inside of the 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)
)
I was wondering if anyone could provide me any insight on this.
Thanks!
Solution
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)
)
I'd also recommend changing the field name of ExerciseName to "name" to avoid confusion.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow