gpt4 book ai didi

scala - Play 2 - Scala - 表单验证器和单选按钮

转载 作者:行者123 更新时间:2023-12-04 17:53:33 26 4
gpt4 key购买 nike

我需要为这个模型渲染一个带有验证器的表单:

模型:

    case class Service (
name: String, description: String, unitcost: Long,
typo: Char, isactive: Char, modifiedby: String)

Controller :
    import play.api.data.Form
import play.api.data._
import play.api.data.format.Formats._
import play.api.data.Forms._

object Services extends Controller {
....
....
private val servicesForm[Service] = Form(
mapping(
"name" -> nonEmptyText.verifying(
"validation.name.duplicate", Service.findByName(_).isEmpty),
"description" -> nonEmptyText,
"unitcost" -> longNumber,
"typo" -> of[Char],
"isactive" -> of[Char],
"modifiedby" -> nonEmptyText
) (Service.apply)(Service.unapply)
)

这段代码在每个 [Char] 上都失败了,说它需要导入 play.api.data.format.Formats._
但我是..
我的第二个疑问是如何为每个(错别字和 isactive)放置一个单选按钮对
认为错字有“M”和“A”之类的选项,而 isactive 有“Y”和“N”。

PD:我认为在使用持久性模型之后...

最佳答案

错误表示Form不知道如何处理Char类型。 Char 没有定义默认值类型。

要解决此问题,您有两种选择:

  • 将类型从 Char 更改为至 String其中默认 Formatter存在
  • 供应FormatterChar

  • 格式化程序看起来像这样(请注意,它没有正确的错误处理)
    implicit val charFormat = new Formatter[Char] {
    def bind(key: String, data: Map[String, String]):Either[Seq[FormError], Char] =
    data.get(key)
    .filter(_.length == 1)
    .map(_.head)
    .toRight(Seq(FormError(key, "error.required", Nil)))

    def unbind(key: String, value: Char) = Map(key -> value.toString)
    }

    关于scala - Play 2 - Scala - 表单验证器和单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13054920/

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