Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Forms 如何处理长和不同格式的发挥!Scala表单?_Forms_Scala_Playframework 2.0 - Fatal编程技术网

Forms 如何处理长和不同格式的发挥!Scala表单?

Forms 如何处理长和不同格式的发挥!Scala表单?,forms,scala,playframework-2.0,Forms,Scala,Playframework 2.0,在我的模型中,所有关联帐户都是长的非正常整数。但是,在新剧中处理Scala表单时!2.0我只能验证表单中的Int数字,而不能验证长 采取以下形式: val clientForm: Form[Client] = Form( mapping( "id" -> number, "name" -> text(minLength = 4), "email" -> optional(text), "phone" -> opti

在我的模型中,所有关联帐户都是
长的
非正常整数。但是,在新剧中处理Scala表单时!2.0我只能验证表单中的
Int
数字,而不能验证

采取以下形式:

val clientForm: Form[Client] = Form(
    mapping(
      "id" -> number,
      "name" -> text(minLength = 4),
      "email" -> optional(text),
      "phone" -> optional(text),
      "address" -> text(minLength = 4),
      "city" -> text(minLength = 2),
      "province" -> text(minLength = 2),
      "account_id" -> number
    )
    (Client.apply)(Client.unapply)
  )

在您看到的
account\u id
中,我想应用一个
Long
,那么如何以最简单的方式进行转换呢?
Client.apply
语法非常简单,但我对映射等选项持开放态度。谢谢

找到了一个非常棒的方法来实现这一点,我在问题中链接的文档中似乎缺少了这个方法

首先,加油!格式:
导入play.api.data.format.Formats.\ucode>

然后在定义表单映射时,使用[]
语法的

然后新的表单val将如下所示:

val clientForm = Form(
    mapping(
      "id" -> of[Long],
      "name" -> text(minLength = 4),
      "address" -> text(minLength = 4),
      "city" -> text(minLength = 2),
      "province" -> text(minLength = 2),
      "phone" -> optional(text),
      "email" -> optional(text),
      "account_id" -> of[Long]
    )(Client.apply)(Client.unapply)
  )
更新:使用可选() 在进一步的实验之后,我发现你可以将[]
中的
与游戏混合在一起
optional
以满足类中定义的可选变量

因此,假设上面的
帐户id
是可选的

"account_id" -> optional(of[Long])

前面的答案肯定有效,但最好只使用
导入play.api.data.Forms.\u
中的内容,因为您已经必须为
可选
文本导入该内容

因此,您可以使用
longNumber

val clientForm = Form(
  mapping(
    "id" -> longNumber,
    "name" -> text(minLength = 4),
    "address" -> text(minLength = 4),
    "city" -> text(minLength = 2),
    "province" -> text(minLength = 2),
    "phone" -> optional(text),
    "email" -> optional(text),
    "account_id" -> optional(longNumber)
  )(Client.apply)(Client.unapply)
)

你的意思是像
number.toLong
?完全正确,只是不需要应用转换。看看我的答案,非常简单!