Search code examples
springthymeleaf

Spring & Thymeleaf form


adding in form is trivial when I have only one object like here:

        <form th:action="|#{group/save}/${id}|" th:object="${groupForm}" method="post" class="col m8 s8 offset-m2">

            <div class="row">
                <div class="input-field">
                    <input th:field="${groupForm.name}" id="name" type="text" required="required"/>
                    <label for="name">Nazwa:</label>
                </div>
            </div>

            <div class="row">
                <button class="btn-success" type="submit" name="save">Wyślij<i class="mdi-content-send right"></i></button>
            </div>
        </form>

but let's assume that groupForm have list of customer

public class Customer{
    private long id;
    private String firstName;
    private String lastName;
    private String nick;
}

How can I add 5 Customers into list in class Group? I want to achive in one request.


Solution

  • Assuming that your Group class looks something like this:

    public class Group {
        private List<Customer> customers;
    }
    

    Try this:

    <input th:field="*{customers[0].name}" type="text" required="required"/>
    <input th:field="*{customers[1].name}" type="text" required="required"/>
    <input th:field="*{customers[2].name}" type="text" required="required"/>
    <input th:field="*{customers[3].name}" type="text" required="required"/>
    <input th:field="*{customers[4].name}" type="text" required="required"/>