3

I have a form containing several fields and some checkboxes. It looks like this now and that is working excelent

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))
)

One thing I want to change from my solution now is that I need at least one of the checkboxes to be checked. As it is now you don't have to check any of them.

I came up with this:

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)))
)

I dont know if this is the right way to go though. It compiles but I don't know how to use the form with the helpers in the template.

Now, when it works without need of checking, it looks like this in the template:

@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"/>
        }

So. Any ideas?

/Regards

4

1 回答 1

1

您可以在此处阅读有关嵌套值的信息。

基本上,这意味着您必须将外部值作为前缀添加到内部值;outer.inner.

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

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

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

分别。

于 2013-06-18T16:52:44.823 回答