2014-12-26 63 views
2

我使用的IntelliJ 14遊戲框架,我認爲框架的版本號爲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> 
    } 
} 

的問題是,我不能讓工作,娛樂不承認鍵入myForm,我應該以某種方式發送它,但它期望一個字符串,而不是一個表單。 我嘗試了幾個例子,但都沒有工作。

Compilation error 

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

[email protected](message: String) 

[email protected](myForm: Form[User]) 

[email protected] helper._ 

[email protected] models.User 

5 

[email protected]("This is login") { 

7 

回答

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)) 
} 
+0

好吧,那些消息是來自遊戲模板,我真的不明白是什麼意思。我會嘗試做修改和測試。非常感謝。 –

+0

我的朋友感謝你,像魅力一樣工作! –