type mismatch; found : play.api.data.Form[controllers.Application.Userdata] required: play.api.data.Form[(String, String)]

1.2k views Asked by At

i have created a simply login submit form using playframework and scala without connecting any database.

my application.scala which is my controller i have written the code

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

and my view template is 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>
    }

but it is not working and giving me error

type mismatch; found : play.api.data.Form[controllers.Application.Userdata] required: play.api.data.Form[(String, String)]

in this line of contoller

 formWithErrors => BadRequest(views.html.login(formWithErrors)), 

what is soultion for my problem??

1

There are 1 answers

5
tuxdna On

Few points to note:

  • Put your case class Userdata outside the Application controller. That is move it inside controller package.
  • Also you can add @import at the beginning of your template, to import any arbitrary package or class.

Try this.

Application.scala: only one Userdata case class definition ( you had two of these classes defined )

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: use Userdata instead of (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>
    }