forms - 如何使用 Playframework scala 绑定(bind)数组表单字段

标签 forms scala binding playframework playframework-2.0

关闭。这个问题是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/

相关文章:

javascript - 使用 Jquery 发布多个表单

scala - 在 Scala 中使用 JsonPath 从 json 字符串中获取特定参数

wpf - 将 Textbox IsFocused 绑定(bind)到 Popup IsOpen 以及附加条件

html - 为什么 CSS 中的文本转换在以表单形式提交时不保留转换(大写)?

javascript - 如何通过 jQuery 和 Ajax 将 Google 表单数据发布到电子表格

javascript - JS+PHP表单验证问题

c# - 将子类绑定(bind)到属性

scala - 如何为 cats Free Monad 中使用的 ADT 创建仿函数

Scala并发放缓

c# - 使用命令式绑定(bind)在 Cosmos DB 中添加文档