将case类应用于嵌套形式Scala Play中的单个字段

时间:2014-04-10 23:15:38

标签: scala playframework playframework-2.0

我正在尝试将案例类应用于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)
  )

我想知道是否有人可以为我提供任何见解。

谢谢!

1 个答案:

答案 0 :(得分:1)

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"避免混淆。