【发布时间】:2013-06-18 19:51:18
【问题描述】:
我有一个包含重复字段的表单:
case class MyForm(topics: List[Int])
val myForm: Form[MyForm] = Form(
mapping(
"topics" -> list(number)
)(MyForm.apply _)(MyForm.unapply _)
)
以及对应的视图:
@form(...) {
<h2>Topics of interest:</h2>
@for(Topic(id, name, _) <- Topics.all) {
@checkbox(
bidForm(s"topics[$id]"),
'_label -> (name + ":").capitalize,
'value -> id.toString)
}
<input type="submit" id="submit" value="Save">
}
到目前为止一切顺利,如果该字段有错误,我会通过 myForm.bindFromRequest 重新渲染它。
我想用我的数据库中的数据预先填写表格。对于其他类型的字段(number、text、option() 等),我可以使用以下内容填充 existingMyForm:
val existingMyForm = myForm.fill(MyForm(
// Queries the database and return a list of case classes with field id: Int
Topics.of(member).map(_.id)
))
但是对于list,这种方法会失败,我必须手动进行映射:
val existingMyForm = myForm.bind(
Topics.of(member).map(t => ("topics[%s]".format(t.id), t.id.toString)).toMap
)
有没有更好的方法来做到这一点?
【问题讨论】:
-
我也有同样的问题!你修好了吗?如果是,如何?
-
不,我还在用同样的hack :(
标签: forms scala playframework-2.0 playframework-2.1