text(minLength = 3),-6ren">
gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:45:54 25 4
gpt4 key购买 nike

我有一个包含多个字段和一些复选框的表单。现在看起来像这样,而且效果很好

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

所以。有任何想法吗?

/问候

最佳答案

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

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

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

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

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

分别。

关于scala - 验证带有复选框元组的映射表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17172432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com