Form values are null in post request in play framework
forms, java, jsp, playframework, playframework-2.0
Solution
Here is how I would do that:
template
@(message: String, contactForm: Form[Contact])
@import helper._
@main("Contact") {
<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/contact.css")">
<div id="pageContainer">
<div id="form">
<div id="topText">
<p>Have a question or need some assistance? Let us know and we'll get back to you ASAP.</p>
</div>
<br/>
@form(routes.Home.contact()) {
<div id="contactInfo">
<label class="contactLabel">First Name:</label> @inputText(contactForm("firstName"), 'class -> "contactInput"') <br />
<label class="contactLabel">Last Name:</label> @inputText(contactForm("lastName"), 'class -> "contactInput"') <br />
<label class="contactLabel">Email:</label> @inputText(contactForm("email"), 'class -> "contactInput"') <br />
<label class="contactLabel">Company:</label> @inputText(contactForm("company"), 'class -> "contactInput"') <br />
<input type="hidden" id="hidden" name="hidden"></input>
<p id="crap">@message</p>
</div>
<br/>
<div id="message">
<label class="contactLabel">Message:</label><textarea cols="50" rows="10"></textarea>
</div>
<input type="submit" value="Submit" />
}
</div>
</div>
}
Class Contact
public class Contact {
public String firstName;
public String lastName;
public String email;
public String company;
// other stuff
}
Controller
public static Result contact() {
Form<Contact> contactForm = form(Contact.class).bindFromRequest();
if (contactForm.hasErrors()) {
// process
} else {
// contactForm.get().firstName should be filled with the correct data
return ok(views.html.contact.render("message", contactForm));
}
}
Does it work for you ? You should take a look at the Play2 documentation about these topics:
- JavaForms
- JavaForm Helpers
Edit
if you can explain why the way I was doing it previously didn't work
I'm not sure about that, but I can see one problem in your code, you don't have to call `data()` on the dynamic form, using directly `DynamicForm.get()` is sufficient, so `first = form.get("firstName")` is sufficient.
Besides as you can see from the DynamicForm Javadoc
get(java.lang.String key)
Gets the concrete value if the submission was a success.
The internal map behind `DynamicForm` has values if there is no errors, so you may check with `hasErrors` before actually getting concrete values from field.
From my point of view, it is better and easier to use the `form(Contact.class).bindFromRequest()` style that will fill an instance of class `Contact`, this without saying that fields can be validated using Java annotations.
Problem
I have html to post back a form, like this: ``` @(message: String) @main("Contact") { <link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/contact.css")"> <div id="pageContainer"> <div id="form"> <div id="topText"> <p>Have a question or need some assistance? Let us know and we'll get back to you ASAP.</p> </div> <br/> <form method="POST" action="@routes.Home.contact()"> <div id="contactInfo"> <label class="contactLabel">First Name:</label><input type="text" name="firstName" id="firstName" class="contactInput" /> <br /> <label class="contactLabel">Last Name:</label><input type="text" name="lastName" id="lastName" class="contactInput" /> <br /> <label class="contactLabel">Email:</label><input type="text" name="email" id="email" firstName" class="contactInput" /> <br /> <label class="contactLabel">Company:</label><input type="text" id="company" name="company" class="contactInput" /> <br /> <input type="hidden" id="hidden" name = "hidden"></input> <p id="crap">@message</p> </div> <br/> <div id="message"> <label class="contactLabel">Message:</label><textarea cols="50" rows="10"></textarea> </div> <input type="submit" name="submit" id="submit" value="Submit"></input> </form> </div> </div> } ``` Back in the controller, it looks like this: ``` public static Result contact() { //Map<String,String[]> values = request().body(); DynamicForm form = form().bindFromRequest(); String first = ""; if(form.data().get("firstName") != null) first = form.data().get("firstName").toString(); return ok(views.html.contact.render(first)); } ``` But when I look through the result of `form().bindFromRequest()`, it is always empty. Am I missing something?