The play docs provide small code snippets on how to use repeated values with form binding, but they make not clear on how to bind to a List of objects. In the end it’s easy.

Let’s say we have these two classes:

[codesyntax lang=“scala“]

case class Task(name: String, description: String)
case class Tasks(tasks: List[Task])

[/codesyntax]

Then we can make a Form val like this:

[codesyntax lang=“scala“]

val taskForm: Form[Tasks] = Form(
      mapping(
          "tasks" -> list(mapping(
              "name" -> text,
              "description" -> text
          )(Task.apply)(Task.unapply))
      )(Tasks.apply)(Tasks.unapply)
  )

[/codesyntax]

The html would look like this:

[codesyntax lang=“html4strict“]

<tr>
  <td><input name="tasks[0].name" type="text" ></td>
  <td><textarea name="tasks[0].description" ></textarea>
  </td>
</tr>
<tr>
  <td><input name="tasks[1].name" type="text" ></td>
  <td><textarea name="tasks[1].description"></textarea>
  </td>
</tr>

[/codesyntax]

And the retrieval as stated in the docs:

[codesyntax lang=“scala“]

val tasks = taskForm.bindFromRequest.get

[/codesyntax]

gives back a list of tasks with a task object inside for each table row.

I admit that my table is not a normal form, instead i use jquerys .serializeArray() on the table to submit the data via $.ajax(), but that’s a different story.

See also this question at stackoverflow from me:

http://stackoverflow.com/questions/10218814/bind-multiple-objects-in-play-framework-2-0-from-a-form/10223431#10223431