2014-12-26 40 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

我的朋友感谢你,像魅力一样工作! –