Scala 验证带有复选框元组的映射表单

Scala 验证带有复选框元组的映射表单,scala,playframework,playframework-2.0,playframework-2.1,Scala,Playframework,Playframework 2.0,Playframework 2.1,我有一个包含几个字段和一些复选框的表单。 现在看来这个和那个都很好用 val postForm = Form( mapping( "author" -> text(minLength = 3), "title" -> text(minLength = 3), "heading" -> text(minLength = 3), "content" -> text(minLength = 5), "tagNews" -> boolean, "ta

我有一个包含几个字段和一些复选框的表单。 现在看来这个和那个都很好用

val postForm = Form(
mapping(
  "author" -> text(minLength = 3),
  "title" -> text(minLength = 3),
  "heading" -> text(minLength = 3),
  "content" -> text(minLength = 5),
  "tagNews" -> boolean,
  "tagBlog" -> boolean
  )((author, title, heading, content, tagNews, tagBlog) => domain.Post(author, title, heading, content, tagNews, tagBlog, None))
   ((post: domain.Post) => Some(post.author, post.title, post.heading, post.content, , post.tagNews, post.tagBlog))
)
我现在想改变解决方案的一点是,我需要至少选中一个复选框。现在你不必检查任何一个

我想到了这个:

val postForm = Form(
mapping(
  "author" -> text(minLength = 3),
  "title" -> text(minLength = 3),
  "heading" -> text(minLength = 3),
  "content" -> text(minLength = 5),
  //TODO: this is not working!
  "tags" -> tuple(
    "tagNews" -> boolean,
    "tagBlog" -> boolean
  ).verifying("One tag must be used", f => f._1 || f._2)
  )((author, title, heading, content, tags) => domain.Post(author, title, heading, content, tags._1, tags._2, None))
   ((post: domain.Post) => Some(post.author, post.title, post.heading, post.content, (post.tagNews, post.tagBlog)))
)
我不知道这是不是正确的方法。它可以编译,但我不知道如何使用模板中的帮助程序的形式

现在,当它在不需要检查的情况下工作时,在模板中如下所示:

@form(presentation.controllers.routes.Post.addPost()){

            @inputText(postForm("author"), '_label -> "", 'placeholder -> "Author", '_showConstraints -> false)
            @inputText(postForm("title"), '_label -> "", 'placeholder -> "Title", '_showConstraints -> false)
            @inputText(postForm("heading"), '_label -> "", 'placeholder -> "Heading", '_showConstraints -> false)
            @textarea(postForm("content"), '_label -> "", 'placeholder -> "Content", '_showConstraints -> false)
            <span class="label label-info">News</span>
            @checkbox(postForm("tagNews"), '_label -> "", '_help -> "")
            <span class="label label-info">Blog</span>
            @checkbox(postForm("tagBlog"), '_label -> "", '_help -> "")

            <input type="submit" class="btn btn-primary btn-success" data-loading-text="Loading..." value="Save Post"/>
        }
@表单(presentation.controllers.routes.Post.addPost()){
@inputText(postForm(“作者”),“\u标签->”,“占位符->“作者”,”\u showConstraints->false)
@inputText(postForm(“标题”),“\u标签->”,“占位符->“标题”,”\u showConstraints->false)
@inputText(postForm(“标题”),“\u标签->”,“占位符->“标题”,“\u显示约束->假)
@textarea(postForm(“内容”),“\u标签->”,“占位符->“内容”,“\u showConstraints->false)
新闻
@复选框(postForm(“tagNews”)、“\u标签->”、“\u帮助->”)
博客
@复选框(postForm(“tagBlog”)、“\u标签->”、“\u帮助->”)
}
所以。有什么想法吗


/关于

您可以阅读有关嵌套值的信息

基本上,这意味着您必须将外部值作为前缀添加到内部值<代码>外部。内部

在你的情况下,你应该使用表格

@checkbox(postForm("tags.tagNews"), '_label -> "", '_help -> "")


分别。

这正是我想要的。我将play doc用作我的圣经,但完全忽略了:“以这种方式使用嵌套数据时,浏览器发送的表单值必须命名为address.street、address.city等。”。谢谢你的帮助!
@checkbox(postForm("tags.tagBlog"), '_label -> "", '_help -> "")