Passing Thymeleaf Checkbox values in Object Model when False

This is an old issue, but we forget these things over time…

The issue is when unchecked Checkbox form elements throw a NullReferenceException with Thymeleaf. The HTML looks like this.

<input th:type="checkbox" th:checked="${flashcard.isStudentFlashcard}" th:name="isStudentFlashcard" id="isStudentFlashcard" class="student-checkbox"/>

Our name and id are set, but that's not enough. I should add that this element is in an Thymeleaf {th:each} iteration, but using the standard rowstat:index approach didn't work either.


A simple solution is in this StackOverflow answer from Ralph.

<input type="hidden" value="on" name="_active"/>

or in our case for a *{flashcard.isStudentFlashcard} field…

<input type="hidden" value="on" name="_isStudentFlashcard"/>

Link to Stackoverflow Answer.