Som*_*tik 5 forms binding scala playframework playframework-2.0
我必须处理的形式有这样的:
<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>
Run Code Online (Sandbox Code Playgroud)
我可以这样得到阵列
request.body.asFormUrlEncoded.get("features[]")
Run Code Online (Sandbox Code Playgroud)
当两个项目都被选中时,它会给我 List(4, 8)
但是当我尝试将其绑定在一个表单中时
case class MyFeatures(features: Seq[Long])
val myForm = Form (
mapping(
"features" -> seq(longNumber)
)(MyFeatures.apply)(MyFeatures.unapply)
)
Run Code Online (Sandbox Code Playgroud)
我总是得到一个空序列,与"features []"相同
编辑
以上示例实际上有效,问题出在其他地方.绑定播放时将要素转换为要素[0] = 4和要素[1] = 8,然后在seq(...)或列表(...)映射中正确处理
尝试:
<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>
Run Code Online (Sandbox Code Playgroud)
编辑
要么:
myForm.bind(myForm.bindFromRequest.data + ("features"-> request.body.asFormUrlEncoded.get("features[]"))).fold(
...
)
Run Code Online (Sandbox Code Playgroud)
这将直接绑定请求中的所有其他字段,然后当涉及到功能时,将手动添加它们.如果您不需要绑定更多数据,那么只需写:
myForm.bind("features"-> request.body.asFormUrlEncoded.get("features[]")).fold(
...
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2625 次 |
| 最近记录: |