将案例类应用于嵌套表单中的单个字段 Scala Play
-
20-12-2019 - |
题
我正在尝试将案例类应用于 Scala Play 表单内的单个字段。我尝试将其应用到的领域是 exerciseName
里面的 setsForm
多变的。
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)
)
我想知道是否有人可以向我提供对此的任何见解。
谢谢!
解决方案
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)
)
我还建议将ExerciseName 的字段名称更改为“name”以避免混淆。
不隶属于 StackOverflow