How to set value for field of type list in scala template in play 2.4?

357 Views Asked by At

I am passing form variable from controller that contain field of type list. How to set user entered value at particular index of list. Below is my code:

@Data
public class TestForm {
    public List<TestInfo> testInfo = new ArrayList<TestInfo>();
    public static class TestInfo {
        public String testName;
        public long testId;
    }
}

public Result testRoute() {
        Form<TestForm> form = Form.form(TestForm.class);
        return ok(test.render(form));
    }

    public Result postRoute() {
        Form<TestForm> form = Form.form(TestForm.class).bindFromRequest();
        TestForm tf = form.get();
        System.out.println(tf.testInfo); // getting empty list
        return ok(tf.testInfo.get(0).testName);
    }
}
@(tests : Form[views.forms.school.TestForm])
@import helper._
<html>
<body>
<form action="@routes.CustomController.postRoute" method="Post">
<input type="text" name=tests("testInfo")(0).testName value="1"/>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

Here i have a TestForm that contains list of type TestInfo. TestInfo contains two field testName, and testId. And i am passing Form of type TestForm to scala template and there i want to set values of testName and testId based on user entered value and and i am binding this form inside controller whenever user press submit. but inside controller i am getting empty value.

1

There are 1 best solutions below

0
JacksonWeekes On

I think you want to use the repeat helper in your view. The code would be something like below(although be aware I haven't actually tested this)

@helper.form(routes.CustomController.postRoute()) {
    @helper.repeat(tests("testInfo"), min = 1) { infoItem =>
        @helper.inputText(infoItem.testName)
        @helper.inputText(infoItem.testId)
    }
}