Lambda Pool
Lambda Pool

Reputation: 39

About play framework validation form

I'm using Play framework on Intellij 14, I think the version of framework is 2 the about info shows me that:

[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

I'm trying to create a form for login, it works well but I can't validate it properly and it shows the error messages. See my code bellow:

POST   /authorize                   controllers.LoginController.authorize

Controller scala:

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

}

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

The problem is, I can't make it work, Play doesn't recognize the type myForm, should I send it somehow but it expects a String, not a form. I tried several examples but none did work.

Compilation error

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

1@(message: String)

2@(myForm: Form[User]) 

3@import helper._

4@import models.User

5

6@main("This is login") {

7

Upvotes: 2

Views: 274

Answers (1)

Michael Zajac
Michael Zajac

Reputation: 55569

You can't declare two parameter lists for a view like that.

@(message: String)
@(myForm: Form[User]) // <-- this doesn't make sense

You need to either collapse them into one list:

@(message: String, myForm: Form[User])

Or group them like this:

@(message: String)(myForm: Form[User])

The templates compiler is only seeing the first list as the parameters, which is why it's getting confused when it sees @(myForm: Form[User]) on the next line.

If you're going to use this Form within the view (which you aren't quite right now, but I assume you will), you'll need to pass the Form object to the view in your index controller function:

def index = Action {
    Ok(views.html.login("ok", loginForm))
}

Upvotes: 2

Related Questions