Aerus
Aerus

Reputation: 4380

Initial values in form in Play 2.0.4

I've created a form in a template profile.scala.html in the Play 2.04 framework. I added several input fields already like this (i.e. for the first name of a user):

@inputText(settings("firstName"), '_label -> "First name:")

This nicely displays the input field with custom label and the constraints.

I would however like some fields to have some initial values, because the user may have filled out some fields before and he is now simply editing his settings a bit.

How can I add these initial values to the input field with my current setup?

Upvotes: 1

Views: 1276

Answers (1)

biesior
biesior

Reputation: 55798

Easiest way in Java is filling your form on the controller side with some new but not saved object:

public static Result editSettings(){
    Settings settings = new Settings();
    settings.firstName = "John";
    settings.lastName = "Doe";
    Form<Settings> settingsForm = form(Settings.class).fill(settings);

    return ok(settings.render(settingsForm));
}

Upvotes: 7

Related Questions