Design Forms In Play2.0 using Scala and Mongodb

Table of contents
Reading Time: 2 minutes

The package contains several helpers to handle HTTP form data submission and validation.

In This blog we would learn how to design forms with validations in play2.0 using scala and how to communicate  with database using mongodb.

Step 1: Create New Project in play2.0 lets say FormDemoInPlay

Step 2: Dependency for Mongodb In Build.scala

[sourcecode language=”scala” wraplines=”false” collapse=”false”]

"com.novus" %% "salat" % "1.9.1"


Step 3: API for login in routes file

routes file contains:


[sourcecode language=”scala” wraplines=”false” collapse=”false”]

POST   /login        controllers.Application.authenticateUser


Step 4: Design a SiginForm with validations.

For this quick sample, let’s define the following view:


[sourcecode language=”scala” wraplines=”false” collapse=”false”]

@import helper._
@import helper.twitterBootstrap._

@title = {
Form Demo in Play2.0

@main(title) {
@helper.form(action = routes.Application.authenticateUser) {

loginForm("email"), ‘_label -> "Email ",
‘_help -> "Enter a valid email address."

‘_label -> "Password",
‘_help -> "A password must be at least 6 characters. "


<input type="submit" value="Log in ">
<small><a href="@routes.Application.siginUpForm">Sign Up </a></small>



Step 5: Code For Controller

The scala version, accessing the body:


[sourcecode language=”scala” wraplines=”false” collapse=”false”]

//Case Class to map with Form

case class LoginForm(email: String, password: String)

object Application extends Controller {

//Play Form for emailid & password
val loginForm = Form(
"email" -> email,
"password" -> nonEmptyText(minLength = 6))(LoginForm.apply)(LoginForm.unapply))

def index = Action {
Ok(views.html.index(loginForm, "Form Demo in Play2.0"))

//Code to authenticate User
def authenticateUser = Action { implicit request =>
errors => BadRequest(views.html.index(errors, "There is some error")),
logInForm => {
val email =
val password = logInForm.password
val users = User.findUser(email, password)
users.isEmpty match {
case true => Ok(views.html.index(loginForm, "Invalid Credentials"))
case false =>
val userEntity = users(0)

Step 6: Code For User Model & For Connectivity with MongoDb

Model code for connectivity with database:


[sourcecode language=”scala” wraplines=”false” collapse=”false”]

import com.mongodb.casbah.commons.MongoDBObject
import org.bson.types.ObjectId
import com.novus.salat.dao.SalatDAO
import com.mongodb.casbah.Imports.WriteConcern
import com.mongodb.casbah.MongoConnection
import com.novus.salat.annotations.Key
import utils.MongoHQConfig

case class UserEntity(
@Key("_id") id: ObjectId,
emailId: String,
password: String)

object User {

* Authenticate User By Credentials Provided
* @param emailId is emailId of user to be searched
* @param password is password of user to be searched
def findUser(emailId: String, password: String): List[UserEntity] = {
UserDAO.find(MongoDBObject("emailId" -> emailId, "password" -> password)).toList

* Instantiate UserDAO
* Access the settings from MongoHQConfig file
object UserDAO extends SalatDAO[UserEntity, ObjectId](collection = MongoHQConfig.mongoDB("user"))[/sourcecode]

Output of above snippets :

Form View :


Validations Applied View :


Entire code can be found here:

3 thoughts on “Design Forms In Play2.0 using Scala and Mongodb3 min read

Comments are closed.