Checkbox Binding PostRequest

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Checkbox Binding PostRequest

Hi everybody, I'm new at Spring Boot and Thymeleaf and I have some troubles with checkboxes.
Explanation : I have 2 Entities Event and User with ManyToMany Relation.
I want to create a Form where a User can create an Event. In this form he could choose a list of User concerned by this Event by checking checkboxes dynamically generated by User Entity (database).

Entity User :

public class User implements Serializable{
        @Id @GeneratedValue
        private Long id_user;
        private String username;
        private String password;
        private String nom;
        private Collection<Event> event;
        //getters setters bla bla bla

Entity Event :

public class Event implements Serializable{
        @Id @GeneratedValue
        private Long id_event;
        private String nom_event;
        private String adresse;
        private Collection<User> users;
        //getters setters bla bla bla

Controller :

        public String createEvent(Model model,User user){
                List<User> listUser= userRepository.findAll();
                model.addAttribute("listUser", listUser);
                List<User> listUserSelected = new ArrayList<>();
                model.addAttribute("listUserSelected", listUserSelected);
                return EventForm;

                bla bla bla
                <th:block th:each="u:${listUser}">
                        <input type="checkbox" name="${u.id_user}" th:value="${u.id_user}"/>       
                        <label th:text="${u.nom}"></label>

                public String saveEvent(Model model, List<User> listUserSelected){

I don't have any troubles to get the user checkboxes. But I don't know how to config checkbox input with Thymeleaf to put datas to listUserSelected and also I didn't find the how to config my post request.

I hope someone can give me the solution :(