简体   繁体   中英

Dynamic size of form in Play framework 2.7 (Java)

I want to create a form application using the Play framework 2.7 in Java and I would like to create all the fields of my form a certain amount of times, which I don't know before creating it.

Here is one of my model :

public class Information {

    public String clientType;
    public String companyName;
    public String name;
    public String firstName;
    public String addressNumber;
    public String apartmentType;

    public Information(String companyName, String name, String firstName, String addressNumber, String clientType,
            String apartmentType) {
        this.clientType = clientType;
        this.companyName = companyName;
        this.name = name;
        this.firstName = firstName;
        this.addressNumber = addressNumber;
        this.apartmentType = apartmentType;
    }
}

Here, one of my data for the form :

public class InformationData {

    @Constraints.Required
    private String clientType;

    @Constraints.Required
    private String companyName;

    @Constraints.Required
    private String name;

    @Constraints.Required
    private String firstName;

    @Constraints.Required
    private String addressNumber;

    @Constraints.Required
    private String apartmentType;

    public InformationData() {
    }

    // getters and setters
}

And here, a part of my view :

@(informationForm: play.data.Form[data.InformationData])
<h1>Informations</h1>

@helper.form(routes.InformationController.validationInformations) {
        @helper.CSRF.formField
        @helper.inputText(
                informationForm("fieldName"),
                Symbol("_help") -> "",
                Symbol("_error") -> informationForm("fieldName").
                   error.map(_.withMessage("ERROR"))
        )
        <button type="submit">Submit</button>
}

In my Controller I have this attribute private final Form<InformationData> form; which will be used as a parameter of my view ( Twirl template).

I tried to transform this attribute as a java.util.List and in my view use a @for loop on this List (which became a Scala Seq in my view) but the validation of the form was not correct : it considered every (for example) name field as the same field, so during validation it would just fill the value of every name field with the value of the first one (even if the first one is empty and the other aren't).

EDIT

I tried to modify the name of all my fields with the current index of the loop (ie : name0 then name1 and so on), it stopped messing with my fields since they don't have the same name, but my InformationData couldn't validate since it can't recognise the names of my fields.

Is it possible to make it understand that name0 and name1 should be considered as (different) name , by using a HashMap or something ?

If you need a list of information, just build the form with a list of information as follow:

public class InformationData {

    @Constraints.Required
    private List<Information> informations;

    // setter and getter
}

In your webpage you can use the list like this:

@(informationForm: play.data.Form[InformationData])

//your html

@for(information<-informationForm.get().getInformations()){
    <p>@information.getName</p>
    <p>@information.getCompanyName</p>
    //all your stuff to print
}

//your html

Remember that to retrieve the fields you should "get()" the form before you can access to the methods inside it.

informationForm.get().getInformations()

To check the length of the list just call the "length" method.

informationForm.get().getInformations.length

For the form part, to build in html you must provide a valid name to build a list of values like

@helper.form(routes.InformationController.validationInformations) {
        @helper.CSRF.formField

        <input type="text" name="name[0]" value="">
        <input type="text" name="companyName[0]" value="">
        .....
        <input type="text" name="name[N]" value="">
        <input type="text" name="companyName[N]" value="">

        <button type="submit">Submit</button>
}

If you can't print it with scala beforehand, you should dynamically create your input fields with some javascript.

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