简体   繁体   中英

Pre-fill form with data Play Framework

I'm using the Play Framework with Java.

I'm trying to pre-fill a form with data and pass it to my template. The issue doesn't seem to be actually filling the form, but more accessing the values after it has been passed to the template.

Model:

public class Job {
    private String title;
    private String employer;

    public void setJobTitle(String title) {
        this.title = title;
    }

    public void setEmployer(String employer) {
        this.employer = employer;
    }

    public String getJobTitle() {
        return title;
    }

    public String getEmployer() {
        return employer;
    }
}

Controller:

public static Result editJob() {

    Job job = new Job();
    job.setJobTitle("foo");
    job.setEmployer("bar");
    Form<Job> jobForm = form(Job.class).fill(job);

    String message = "Edit Job";
    return ok(editJob.render(message, jobForm));
}

View:

@(message: String, jobForm: play.data.Form[model.Job]) {

    @helper.form(action = routes.JobController.editJob(), 'enctype -> "multipart/form-data", 'name -> "jobForm"){
      <input type="text" name="jobTitle" id="jobTitle" value="@jobForm.getJobTitle">
      <input type="text" name="employerName" id="employerName" value="@jobForm.getEmployer">
   }
}

Could somebody please explain how to access the data in the view template.

Fix:

@helper.inputText(jobForm(jobForm.get().getJobTitle))

Thanks,

S

Do @jobForm.get().title

you can also use helper input text field

@inputText(jobForm("title"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM