robert trudel
robert trudel

Reputation: 5749

append attribute for the first element in thymeleaf

I try in thymeleaf 3 to add required when it's the first element. I have already attribute name.

<div th:each="role, iter : ${roles}" th:remove="tag">
    <input type="checkbox"  th:attr="name='roles[]'" th:attrappend="${iter.first ? 'required' : ''}"  th:value="${role.id}" th:text="${role.name}">
</div>

I get this error

org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as assignation sequence: "${iter.first ? 'required'}"

Upvotes: 0

Views: 248

Answers (1)

Metroids
Metroids

Reputation: 20477

No need for th:attrappend here, just use th:required. I would format it like this:

<th:block th:each="role, iter : ${roles}">
    <input type="checkbox"  name="roles[]" th:required="${iter.first}" th:value="${role.id}" th:text="${role.name}" />
</th:block>

If you want to use th:attrappend, you have to format with an assignment, like this:

<th:block th:each="role, iter : ${roles}">
    <input type="checkbox"  name="roles[]" th:attrappend="required=${iter.first} ? 'required'" th:value="${role.id}" th:text="${role.name}" />
</th:block>

Upvotes: 1

Related Questions