从表单中发挥Framework 2.0中绑定多个对象 [英] bind multiple objects in play framework 2.0 from a form

查看:169
本文介绍了从表单中发挥Framework 2.0中绑定多个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拼命想从一个表单提交接收值的列表,并将其绑定到对象的列表。

i am desperately trying to receive a list of values from a form submission and bind it to a list of objects.

什么作品是检索单个行:

What works is to retrieve a single row:

//class
case class Task(name: String, description: String)

val taskForm: Form[Task] = Form(
  mapping(
  "name" -> text,
  "description" -> text

  )(Task.apply)(Task.unapply)
)


//form
<tr>
  <td><input name="name" type="text" class="span2" placeholder="Name..."></td>
  <td><textarea name="description" class="autoexpand span7" rows="1"     placeholder="Description..."></textarea>
  </td>
</tr>

//receiving action:
val task = taskForm.bindFromRequest.get

但现在我想提交任务类型的多个对象,像这样,例如:

But now i want to submit multiple objects of type task like this for instance:

<tr>
  <td><input name="name[0]" type="text" class="span2" placeholder="Name..."></td>
  <td><textarea name="description[0]" class="autoexpand span7" rows="1" placeholder="Description..."></textarea></td>                   
</tr>
<tr>
  <td><input name="name[1]" type="text" class="span2" placeholder="Name..."></td>
  <td><textarea name="description[1]" class="autoexpand span7" rows="1" placeholder="Description..."></textarea></td>                   
</tr> 

做一个
taskForm.bindFromRequest.get
现在失败。

Doing a taskForm.bindFromRequest.get now fails.

难道有人想出了一个解决的办法?或者你怎样对待这样的情况完全不同?

Did somebody come up with a solution to this? Or do you handle such a situation totally different?

推荐答案

好了,谢谢你暗示我的文档再看看,我见过他们了,但永远无法弥补如何结合起来,使其工作。我想这是因为我是一个总的Scala小白。
但是,我得到它再次给它一段时间后的工作,这是我的解决方案:

Well, thanks for hinting me to look at the docs again, i've seen them already, but never could make up how to combine it to make it work. I think this is because i am a total scala noob. However, i got it working after giving it some time again, this is my solution:

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

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

//form
<tr>
  <td><input name="tasks[0].name" type="text" class="span2" placeholder="Name..."></td>
  <td><textarea name="tasks[0].description" class="autoexpand span7" rows="1" placeholder="Description..."></textarea></td>                   
</tr>
<tr>
  <td><input name="tasks[1].name" type="text" class="span2" placeholder="Name..."></td>
  <td><textarea name="tasks[1].description" class="autoexpand span7" rows="1" placeholder="Description..."></textarea></td>                   
</tr>

和最后做了:

val tasks = taskForm.bindFromRequest.get

检索任务列表

这篇关于从表单中发挥Framework 2.0中绑定多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆