Search code examples
javaspring-mvcthymeleaf

Thymeleaf - enum value cannot be bind


I have a problem with binding value to enum field in thymeleaf

<input type="hidden" th:field="${petDto.rating.finalRating}" th:value="${T(net.azurewebsites.mypet.domain.ratings.Scale).COOL}" />

I want to set enum value Scale.Cool to field finalRating which has type Scale. But in this way its not working. Thymeleaf do not bind value from th:value to th:field. Any idea?


Solution

  • th:field and th:value cannot work together in one input. th:field effectively replaces value, id and the name of the input. So your input is equivalent to

    <input type="hidden" id="rating.finalRating" name="rating.finalRating" th:value="${petDto.rating.finalRating}" />
    

    To achieve what you want, just omit th:field from the input.

    Please refer to the documentation of thymeleaf inputs