NaN
NaN

Reputation: 3591

java spring thymeleaf: bind object with enum-list-attribute attribute to html form

I have a entity containing a list of possible shipping options:

//ShippingType.java
public enum ShippingType {
    DOWNLOAD,
    SHIPPING
}


//SoftwareType.java
@Entity
public class SoftwareType {

    //...
    @Column(unique = true)
    private String name;

    @ElementCollection(targetClass=ShippingType.class)
    @CollectionTable(name = "softwaretype_shippingtype", joinColumns = @JoinColumn(name = "softwaretype_id"))
    @Column(name = "shippingtype", nullable = false)
    @Enumerated(EnumType.STRING)
    private List<ShippingType> supportedShippingTypes;

    //...

    public List<ShippingType> getSupportedShippingTypes() {
        return supportedShippingTypes;
    }

    public void setSupportedShippingTypes(List<ShippingType> supportedShippingTypes) {
        this.supportedShippingTypes = supportedShippingTypes;
    }
}

Now I want to bind the object with thymeleaf to a html-form to easily create/edit such entities.

<div class="panel-body" th:fragment="createUpdateForm(action, SoftwareType)">
    <form role="form" action="#" th:action="@{${action}}" th:object="${SoftwareType}" method="post">

        <label for="softwareTypeName">Name</label>
        <input type="text" th:field="*{name}" id="softwareTypeName"/>

        <!-- how to insert checkboxes of shippingTypes ?? -->
        <button type="submit" value="submit"/>
    </form>
</div>

But how can I insert a list of checkboxes with all ShippingTypes and bind it to the object?

Upvotes: 1

Views: 5024

Answers (1)

lgd
lgd

Reputation: 1212

You could iterate over your enum ShippingType, e.g. :

<div th:each="shippingType : ${T(com.example.ShippingType).values()}">
    <input type="checkbox" th:id="${{shippingType}}" th:value="${{shippingType}}" th:field="*{supportedShippingTypes}" />
    <label th:for="${{shippingType}}">Shipping Types</label>
</div>

Upvotes: 5

Related Questions