【问题标题】:Why the value cannot be found for Scala form?为什么找不到 Scala 表单的值?
【发布时间】:2014-03-01 14:13:28
【问题描述】:

我有以下 Scala 类,我认为它应该定义表单的结构和约束

package controllers

import play.api.data._
import play.api.data.Forms._
class UserLogin{
case class UserLogin (username: String, password: String)
val userForm= Form(mapping("username" -> nonEmptyText(5,25),"password" -> nonEmptyText(5,25))(UserLogin.apply)(UserLogin.unapply)
)
}

然后我还有这个 Scala.html 文件用于构造表单

@import helper._
@index("Login")

@(userForm: play.data.Form[UserLogin])
@helper.form(action = routes.Application.home) {
@helper.inputText(userForm("username"). id -> "username")
@helper.inputPassword(userForm("password"), id -> "password")
}

一直在用教程帮我http://www.playframework.com/documentation/2.2.x/ScalaForms

问题在于这里的 scala.html 文件@(userForm: play.data.Form[UserLogin]) userForm 给出以下编译错误not found: value userForm

任何解决此问题的建议将不胜感激。

【问题讨论】:

  • 为什么在caseclass之后使用{}?你的控制器方法在哪里
  • 我不太确定 scala 语法,但尝试过class UserLogin { case class UserLogin (username: String, password: String) val userForm= Form(mapping("username" -> nonEmptyText(5,25),"password" -> nonEmptyText(5,25))(UserLogin.apply)(UserLogin.unapply) ) } 但有同样的问题。你指的是哪个控制器类?

标签: forms scala playframework compiler-errors


【解决方案1】:

我认为您的主要问题是 HTML 模板中的语法不正确,而不是您的案例类。

根据documentation,您的模板参数需要在模板文件的顶部声明:

@(userForm: play.data.Form[UserLogin])

后跟任何导入语句:

@import helper._

然后是 HTML 模板的其余部分。

假设index 是一个主要布局模板,它接受String 和一些Html,我认为您的HTML 模板需要看起来像这样:

@(userForm: play.data.Form[UserLogin])

@import helper._

@index("Login") {

  @helper.form(action = routes.Application.home) {
    @helper.inputText(userForm("username"). id -> "username")
    @helper.inputPassword(userForm("password"), id -> "password")
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-24
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多