关于play框架验证表单

时间:2014-12-26 20:57:50

标签: scala intellij-idea playframework

我在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

控制器scala:

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

1 个答案:

答案 0 :(得分:2)

您无法为此类视图声明两个参数列表。

@(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))
}