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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With