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))
)
有一兩件事我想從我的解決辦法改變現在是我需要的複選框中的至少一個進行檢查。現在你不必檢查它們中的任何一個。
我想出了這一點:
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"/>
}
所以。有任何想法嗎?
/問候
這正是我所期待的。我使用play doc作爲我的聖經,但完全錯過了:「當你以這種方式使用嵌套數據時,瀏覽器發送的表單值必須被命名爲address.street,address.city等。」謝謝你的幫助! – korrekorre