关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。
想改进这个问题?将问题更新为 on-topic对于堆栈溢出。
7年前关闭。
Improve this question
我必须处理的表格是这样的:
<label for="features_1">
<input type="checkbox" id="features_1" name="features[]" value="4"> foo
</label>
<label for="features_2">
<input type="checkbox" id="features_2" name="features[]" value="8"> bar
</label>
我可以像这样得到数组
request.body.asFormUrlEncoded.get("features[]")
当两个项目都被选中时,这给了我
List(4, 8)
但是当我尝试将它绑定(bind)在一个表单中时
case class MyFeatures(features: Seq[Long])
val myForm = Form (
mapping(
"features" -> seq(longNumber)
)(MyFeatures.apply)(MyFeatures.unapply)
)
我总是得到一个空序列,与“features []”相同
编辑
上面的例子确实有效,问题出在其他地方。绑定(bind)后 play 将特征转换为 feature[0]=4 和 features[1]=8 然后在 seq(...) 或 list(...) 映射中正确处理
最佳答案
尝试:
<label for="features_1">
<input type="checkbox" id="features_1" name="features[0]" value="4"> foo
</label>
<label for="features_2">
<input type="checkbox" id="features_2" name="features[1]" value="8"> bar
</label>
编辑
或者:
myForm.bind(myForm.bindFromRequest.data + ("features"-> request.body.asFormUrlEncoded.get("features[]"))).fold(
...
)
这将直接绑定(bind)请求中的所有其他字段,然后在涉及功能时,将手动添加它们。
如果您不需要绑定(bind)更多数据,则只需编写:
myForm.bind("features"-> request.body.asFormUrlEncoded.get("features[]")).fold(
...
)
关于forms - 如何使用 Playframework scala 绑定(bind)数组表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23105619/