shashank
shashank

Reputation: 399

Forms in Scala play framework

Hello i am a beginner to scala play framework. I am unable to create form with two or more inputs. i googled it and found none in scala programming language. Kindly suggest me an idea regarding how to create multiple inputs in a form using scala. i did this

val form = Form (tuple
    (
"firstname"-> text,
"lastname" -> text
)
)  and to get the values val(fname,lname) = form.bindFromRequest.get

am i following correct way. Kindly suggest me any idea or resource to learn scala play framework . Thanks in advance

Upvotes: 6

Views: 12062

Answers (2)

Nagarjuna Pamu
Nagarjuna Pamu

Reputation: 14825

Play documentation is the best way to learn about forms https://www.playframework.com/documentation/2.1.1/ScalaForms if you want more then, please have a look at play-example-form project.

http://typesafe.com/activator/template/play-example-form This activator example project on forms explains everything about forms.

1) It explains forms and data binding, validation in the play controller.

2) It explains about Optional parameters in the forms.

3) It explains about complex forms with nested objects for example

     case class Student(name: String, age: Int, gender: Optional[Char] = None, 
                                                address: Address, other: OtherStuff)

The above case class depends on Address, OtherStuff and notice that the gender is optional. The example project explains how to deal with such complex object dependencies.

Please download activator from here http://typesafe.com/get-started . then, start the activator in browser UI mode using the command activator ui and type play-example-form in the search to download it. After downloading go to the project root folder and type activator eclipse in case you use eclipse or activator gen-idea in case you Intellij Idea.

Upvotes: 0

Matt Roberts
Matt Roberts

Reputation: 1107

Here is a complete (but simple) form example for Play 2.1.1. Including the view, controller and routes file. I suspect you were missing imports and / or an implicit request. Both of which would be understandable!

The controller (Application.scala):

package controllers

import play.api._
import play.api.mvc._
import play.api.data._
import play.api.data.Forms._

object Application extends Controller {
  val form = Form(
    tuple(
      "firstname" -> text,
      "lastname" -> text
    )
  )

  def index = Action {
    Ok(views.html.index())
  }

  def submit = Action { implicit request =>
    val (fname, lname) = form.bindFromRequest.get
    Ok("Hi %s %s".format(fname, lname))
  }
}

The view (index.scala.html):

<!DOCTYPE html>
<html>
  <head>
    <title>Form example</title>
  </head>
  <body>
    <form method="post" autocomplete="on">
      First name:<input type="text" name="firstname"><br>
      Last name: <input type="text" name="lastname"><br>
      <input type="submit">
    </form>
  </body>
</html>

And the routes:

GET     /                           controllers.Application.index
POST    /                           controllers.Application.submit

NB: The name attributes in your HTML view must match the string literals in your controller form.

Hope that helps.

Upvotes: 26

Related Questions