0

Simple problem but can't find a solution: I have a Thymeleaf form used to add a new object, say of a Book class. It works perfectly well and I only need that particular form for adding new objects, not editing the existing ones. The question is: how can I put several objects of the Book class in the same single form? So, purely for convenience, instead of filling form for a single book and clicking Send you can fill form for several books at once and only then click Send, have them all inserted into the database (in whatever order) and also have the option to fill the form partially (e.g. the form has room for 5 books but it will also accept 1, 2, 3 or 4 and you can leave the rest blank).

Edit: I've tried passing a list of object to the Thymeleaf template with the form bound to the whole list and iteration inside, but Thymeleaf throws BingingResultError upon rendering it.

PaulJK
  • 189
  • 11
  • Does this answer your question? [HTML form containing multiple 'objects'](https://stackoverflow.com/questions/36519952/html-form-containing-multiple-objects). That question uses PHP - but the approach is the same regardless of server-side technology. In your form, the IDs must be different, but the `name` attributes can be repeated to build arrays. – andrewJames Aug 28 '20 at 12:25
  • It doesn't, sorry. With Thymeleaf you declare single object for an entire form like this:
    – PaulJK Aug 28 '20 at 12:46
  • Can that object not be an arraylist - let's call it `${books}` - backed by a Java `List`? – andrewJames Aug 28 '20 at 12:50
  • Tried that, I get a binding result error when the form is bound to a list and not to a single object. – PaulJK Aug 28 '20 at 12:53
  • OK, understood - can I suggest you [edit] the question to show your code for that - (a) the form, (b) the binding, and (c) the error message, and any other details that may help troubleshooting. – andrewJames Aug 28 '20 at 12:57
  • You can't back a form with a `List`, but you CAN back a form with an object that has a `List` as a property, and it should work for your needs. – Metroids Aug 28 '20 at 14:40

1 Answers1

1

You need to use a wrapper object to realize what you want. Something like:

public class BooksCreationDto {
    private List<Book> books;
 
    // default and parameterized constructor
 
    public void addBook(Book book) {
        this.books.add(book);
    }
    
    // getter and setter
}

Then you need to pass this object as a model attribute in your controller:

BooksCreationDto booksForm = new BooksCreationDto();
model.addAttribute("form", booksForm);

bind fields using index property

   th:field="*{books[__${itemStat.index}__].title}"

and get back the result with

 @ModelAttribute BooksCreationDto form 

in your controller.

For a complete and detailled explaination visit: https://www.baeldung.com/thymeleaf-list

Xtof
  • 415
  • 5
  • 11
  • Yup, that works, thank you. Just had to play with the dto and form a little and make sure that I initialize the list in dto's controller. – PaulJK Aug 29 '20 at 10:44