1
votes

I am working with Thymeleaf and trying to do some object binding, but I do not know how to do it if I have an object with a list. Let me explain:

My model:

@Entity
public class Project {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull
    private String name;

    @NotNull
    @Lob
    private String description;

    @NotNull
    private Date startDate;

    private String status;

    @ManyToMany
    private List<Role> rolesNeeded;

    @ManyToMany
    private List<Collaborator> collaborators;

    public Project() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public List<Role> getRolesNeeded() {
        return rolesNeeded;
    }

    public void setRolesNeeded(List<Role> rolesNeeded) {
        this.rolesNeeded = rolesNeeded;
    }

    public List<Collaborator> getCollaborators() {
        return collaborators;
    }

    public void setCollaborators(List<Collaborator> collaborators) {
        this.collaborators = collaborators;
    }
}

My html form:

<form method="post" action="addproject" th:object="${project}">
    <div>
        <label for="project_name"> Project Name:</label>
        <input th:field="*{name}" type="text" name="project_name"/>
    </div>
    <div>
        <label for="project_description">Project Description:</label>
        <textarea th:field="*{description}" rows="4" name="project_description"></textarea>
    </div>
    <div>
        <label for="project_status">Project Status:</label>
        <div class="custom-select">
            <span class="dropdown-arrow"></span>
            <select th:field="*{status}" name="project_status">
                <option value="active">Active</option>
                <option value="archived">Archived</option>
                <option value="not_started">Not Started</option>
            </select>
        </div>
    </div>
    <div>
        <label for="project_roles">Project Roles:</label>
        <ul class="checkbox-list">
            <li th:each="role : ${roles}">
                <input th:field="*{rolesNeeded}" type="checkbox" name="project_roles" th:value="${role}"/>
                <span class="primary" th:text="${role.name}"> Developer</span>
            </li>
        </ul>
    </div>
    <div class="actions">
        <input type="submit" value="Save" class="button"/>
        <a href="#" class="button button-secondary">Cancel</a>
    </div>
</form>

And I am getting the error:

ERROR!!!!: Field error in object 'project' on field 'rolesNeeded': rejected value [com.imprender.instateam.model.Role@145d6cd4,com.imprender.instateam.model.Role@73020d6f]; codes [typeMismatch.project.rolesNeeded,typeMismatch.rolesNeeded,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [project.rolesNeeded,rolesNeeded]; arguments []; default message [rolesNeeded]]; default message [Failed to convert property value of type 'java.lang.String[]' to required type 'java.util.List' for property 'rolesNeeded'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.imprender.instateam.model.Role' for property 'rolesNeeded[0]': no matching editors or conversion strategy found]

Basically, as far as I understood, the checkbox input returns a String[], but my object needs a list, so the binding cannot be perfomed.

How could I bind the array in the list? (do you have an example?)

Thank you.

1

1 Answers

0
votes

If Your Role bean has active boolean property, You could do something like this (simplified):

<ul class="checkbox-list">
  <li th:each="role,stat : ${project.rolesNeeded}">
    <input th:field="*{rolesNeeded[__${stat.index}__].active}" type="checkbox"/>
    <input th:field="*{rolesNeeded[__${stat.index}__].name}" type="hidden"/>
    <span class="primary" th:text="${role.name}">Developer</span>
  </li>
</ul>

If it does not, you could store the rolesNeeded in the hidden fields and populate them with javascript.