I have Project
object that contains the list Roles
with Role
object. I want to edit the project using the form. This form need to have checklist of roles. So I have binded Project
object with th:object
. Now when the user selects the check boxes and saves, it is failing with the error.
Form to render Check box
<ul class="checkbox-list">
<li th:each="role,stat : ${allRoles}">
<input type="checkbox" th:value="${role.id}" th:field="*{roles[__${stat.index}__].id}"/>
<span class="primary" th:text="${role.name}"> Developer</span>
</li>
</ul>
Project Object
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
int id;
String name;
String description;
private String status;
private String slug;
@ManyToMany
List<Role> roles = new ArrayList<>();
@ManyToMany
List<Collaborator> collaborators = new ArrayList<>();
................
................
}
Role Object:
@Entity
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY )
private int id;
private String name;
.............
.............
.............
}
Relevant Controller Methods
@RequestMapping("/edit-project/{slug}")
public String editProjectFromSlug(@PathVariable String slug,Model model) {
Project project = projectService.findProjectBySlug(slug);
List<Role> allRoles = roleService.getAllRoles();
//Two objects are added to the model
model.addAttribute("project",project);
model.addAttribute("allRoles",allRoles);
return "edit_project";
}
@RequestMapping(value = "/save-project", method = RequestMethod.POST)
public String saveProject(@ModelAttribute Project project,Model model){
projectService.saveProject(project);
return "redirect:/projects";
}
I have referred multiple answers like this. None of them are working. The code is at this github repo. Please help me to fix this error.