중첩된 양식의 개별 필드에 케이스 클래스 적용 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