类型不匹配;发现:play.api.data.Form [controllers.Application.Userdata]必需:play.api.data.Form [(String,String)]

时间:2014-11-23 16:35:07

标签: scala playframework eclipse-kepler

我使用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)), 

我的问题的灵魂是什么?

1 个答案:

答案 0 :(得分:-1)

几点需要注意:

  • 将您的案例类Userdata放在Application控制器之外。那就是把它移到控制器包里面。
  • 您还可以在模板的开头添加@import,以导入任意包或类。

试试这个。

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>
    }