我使用playframework和scala创建了一个简单的登录提交表单,而没有连接任何数据库。
我的application.scala
这是我的控制器,我编写了代码
import play.api._
import play.api.mvc._
import play.api.data._
import play.api.data.Forms._
import play.api.mvc.Request
import views.html._
case class Userdata(email:String,password:String)
object Application extends Controller {
case class Userdata(email:String,password:String)
val userForm = Form(
mapping(
"email" -> text,
"password" -> text
)(Userdata.apply)(Userdata.unapply)
)
def usercheck = Action{ implicit request=>
userForm.bindFromRequest.fold(
formWithErrors => BadRequest(views.html.login(formWithErrors)),
Userdata => { Ok(views.html.index("congrates"))}
)}
def index() = Action {
implicit request=>
Ok(views.html.login(userForm))
}
我的视图模板是login.scala.html
..
@(form: Form[(String,String)])
@import helper._
@form(routes.Application.usercheck){
<form class=frmuuser>
<input type=text placeholder=email id='emailid'>
<input type=password placeholder=password id='pwd'>
<input type=submit value=login id='enter'>
</form>
}
但它不起作用并给我错误
type mismatch; found : play.api.data.Form[controllers.Application.Userdata] required: play.api.data.Form[(String, String)]
在这行控制器中
formWithErrors => BadRequest(views.html.login(formWithErrors)),
我的问题的灵魂是什么?
答案 0 :(得分:-1)
几点需要注意:
Userdata
放在Application控制器之外。那就是把它移到控制器包里面。试试这个。
Application.scala
:只有一个Userdata
案例类定义(您定义了两个这样的类)
import play.api._
import play.api.mvc._
import play.api.data._
import play.api.data.Forms._
import play.api.mvc.Request
import views.html._
case class Userdata(email:String, password:String)
object Application extends Controller {
val userForm = Form(
mapping(
"email" -> text,
"password" -> text
)(Userdata.apply)(Userdata.unapply)
)
def usercheck = Action{ implicit request=>
userForm.bindFromRequest.fold(
formWithErrors => BadRequest(views.html.login(formWithErrors)),
Userdata => { Ok(views.html.index("congrates"))}
)}
def index() = Action {
implicit request=>
Ok(views.html.login(userForm))
}
login.scala.html
:使用Userdata
代替(String, String)
@(form: Form[Userdata])
@import helper._
@form(routes.Application.usercheck){
<form class=frmuuser>
<input type=text placeholder=email id='emailid'>
<input type=password placeholder=password id='pwd'>
<input type=submit value=login id='enter'>
</form>
}