Play! framework - handle a POST request
Asked Answered
C

2

10

this is the route to handle the login POST request:

POST  /login/submit                 controllers.Users.loginSubmit(user : String, password : String)

this is the login.scala.html:

<form method="post" action="???">
  <input type="text" name="username" /><br/>
  <input type="password" name="password" /><br/>

  <input type="submit" value="Login" />
</form>

I got two questions:

  1. what should be the value of action? is it "login/submit"?
  2. how do you pass this form to be handled in the loginSubmit function?

thanks

Crenate answered 21/8, 2012 at 16:22 Comment(0)
A
26

If it's POST form, you don't need to declare params in the route:

POST  /login/submit           controllers.Users.loginSubmit()

Template:

<!-- syntax: @routes.ControllerName.methodName() -->
<form method="post" action="@routes.Users.loginSubmit()">
  <input type="text" name="username" /><br/>
  <input type="password" name="password" /><br/>

  <input type="submit" value="Login" />
</form>

Import:

import play.data.DynamicForm;
import play.data.Form;

Controller:

public static Result loginSubmit(){
    DynamicForm dynamicForm = Form.form().bindFromRequest();
    Logger.info("Username is: " + dynamicForm.get("username"));
    Logger.info("Password is: " + dynamicForm.get("password"));
    return ok("ok, I recived POST data. That's all...");
}

Template form helpers

There are also form template helpers available for creating forms in Play's template so the same can be done as:

@helper.form(action = routes.User.loginSubmit()) {
    <input type="text" name="username" /><br/>
    <input type="password" name="password" /><br/>

    <input type="submit" value="Login" />
}

They are especially useful when working with large and/or pre-filled forms

Akanke answered 21/8, 2012 at 16:49 Comment(3)
Does it work also for Scala? For me doesn't work with POST and I have to switch to GET requests, because there is nothing in the object requestShrine
Getting the form didn't work for me with "form().bindFromRequest();" Got to use "Form.form().bindFromRequest();" as shown here playframework.com/documentation/2.3.x/JavaFormsDeedee
Answer was written for Play 2.0.x and in 2.1.x it was refactored by the team, anyway that's not problem as well as you can just use proper import... import static play.data.Form.form;Akanke
S
5

In Play Framework version 2.5.x Form.form() is deprecated and you should use inject a FormFactory

Here you can find example: The method form(Class) from Form class is deprecated in Play! Framework

Import:

import play.data.DynamicForm;
import play.data.FormFactory;

Inject:

@Inject FormFactory formFactory;

Controller:

public static Result loginSubmit(){
    DynamicForm dynamicForm = formFactory.form().bindFromRequest();
    Logger.info("Username is: " + dynamicForm.get("username"));
    Logger.info("Password is: " + dynamicForm.get("password"));
    return ok("ok, I recived POST data. That's all...");
}
Sismondi answered 4/7, 2017 at 5:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.