【问题标题】:About play framework validation form关于播放框架验证表
【发布时间】:2014-12-27 09:31:17
【问题描述】:

我在 Intellij 14 上使用 Play 框架,我认为框架的版本是 2,关于信息告诉我:

[info] The current project is built against Scala 2.10.4
[info] Available Plugins: sbt.plugins.IvyPlugin, sbt.plugins.JvmPlugin, sbt.plugins.CorePlugin, sbt.plugins.JUnitXmlReportPlugi
n, play.Play, play.PlayJava, play.PlayScala, play.twirl.sbt.SbtTwirl, com.typesafe.sbt.jse.SbtJsEngine, com.typesafe.sbt.jse.Sb
tJsTask, com.typesafe.sbt.web.SbtWeb, com.typesafe.sbt.webdriver.SbtWebDriver, com.typesafe.sbt.coffeescript.SbtCoffeeScript, c
om.typesafe.sbt.less.SbtLess, com.typesafe.sbt.jshint.SbtJSHint, com.typesafe.sbt.rjs.SbtRjs, com.typesafe.sbt.digest.SbtDigest
, com.typesafe.sbt.mocha.SbtMocha, com.typesafe.sbteclipse.plugin.EclipsePlugin, org.sbtidea.SbtIdeaPlugin, com.typesafe.sbt.Sb
tNativePackager
[info] sbt, sbt plugins, and build definitions are using Scala 2.10.4

我正在尝试创建一个用于登录的表单,它运行良好,但我无法正确验证它并显示错误消息。请参阅下面的代码:

POST   /authorize                   controllers.LoginController.authorize

控制器缩放:

package controllers

import models.{DB, User}
import play.api.data.Form
import play.api.data.Forms._
import play.api.libs.json.Json
import play.api.mvc._


/**
 * Created by jlopesde on 26-12-2014.
 */
object LoginController extends Controller {

  val loginForm: Form[User] = Form (
    mapping (
      "user" -> nonEmptyText,
      "password" -> nonEmptyText
    )(User.apply)(User.unapply)
      verifying ("Invalid login", f => true)
  )

  def index = Action {
    Ok(views.html.login("ok"))
  }

  def authorize = Action {implicit request =>
    // get user from request
    val user = loginForm.bindFromRequest().get
    // query user database
    val _user = DB.query[User].whereEqual("login", user.login).whereEqual("password", user.password).fetchOne()
    var response = _user match {
      case None => "KO"
      case _ => "OK"
    }
    if (response.equals("OK")) {
      //send to index page
      Redirect(routes.Application.index()).withSession("user" -> user.login)
    } else {
      Redirect(routes.LoginController.index())
    }
  }

}

login.html:

@(message: String)
@(myForm: Form[User])
@import helper._
@import models.User

@main("This is login") {

@helper.form(routes.LoginController.authorize()) {
        <label for="user">username:</label> <input id="user" name="user" type="text">
        <label for="password">Password:</label><input id="password" name="password" type="password">
        <button>Login</button>
    }
}

问题是,我无法让它工作,Play 无法识别类型 myForm,我应该以某种方式发送它,但它需要一个字符串,而不是一个表单。 我尝试了几个示例,但都没有奏效。

Compilation error

not found: value myForm
In C:\Users\jlopesde\playprojects\my-first-app\app\views\login.scala.html:2

1@(message: String)

2@(myForm: Form[User]) 

3@import helper._

4@import models.User

5

6@main("This is login") {

7

【问题讨论】:

    标签: scala intellij-idea playframework


    【解决方案1】:

    您不能为这样的视图声明两个参数列表。

    @(message: String)
    @(myForm: Form[User]) // <-- this doesn't make sense
    

    您需要将它们折叠成一个列表:

    @(message: String, myForm: Form[User])
    

    或者像这样分组:

    @(message: String)(myForm: Form[User])
    

    模板编译器只将第一个列表视为参数,这就是为什么当它在下一行看到@(myForm: Form[User]) 时会感到困惑。

    如果您要在视图中使用此 Form(您现在还不太会,但我假设您会这样做),您需要将 Form 对象传递给您的视图中的视图index控制器函数:

    def index = Action {
        Ok(views.html.login("ok", loginForm))
    }
    

    【讨论】:

    • 哦,这些消息来自 play 模板,我不太明白是什么意思。我会尝试进行修改和测试。非常感谢。
    猜你喜欢
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    • 1970-01-01
    • 1970-01-01
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多