3

いくつかのフィールドといくつかのチェックボックスを含むフォームがあります。今はこのように見えますが、それはうまく機能しています

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

今のソリューションから変更したいことの 1 つは、少なくとも 1 つのチェックボックスをオンにする必要があることです。今のところ、それらのいずれもチェックする必要はありません。

私はこれを思いついた:

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

そう。何か案は?

/よろしく

4

1 に答える 1

1

ネストされた値については、こちらを参照してください。

基本的には、外側の値をプレフィックスとして内側の値に追加する必要があることを意味します。outer.inner.

あなたの場合、フォームを使用する必要があります

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

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

それぞれ。

于 2013-06-18T16:52:44.823 に答える