0

Does anyone know how to add a default setting of checked in a Thymeleaf, SpringBoot, Java project, to an input field for a Single checkbox? I have to use th:field because I am later submitting this form via email and if I use th:name with the checked box default, the default works but email doesn't.

Any suggestions?

<label>
       <input type="checkbox" id="serviceLevel"  th:field="*{serviceLevel}" th:checked="${serviceLevel}"/>
Affiliate Serviced</label>

Email html code to pull value on email:

<tr class="emailRow">
  <h3>Direct: </h3>
    <td class="bodycopy" th:text="${directBind.directBox}">
    </td>
</tr>

I have this variable set as private Boolean directBox in the model.

I've tried everything: th:checked=checked, value=true, value=checked, set the value and checked ="${directBox}" none of this works.

Is there a solution out there?

DimaSan
  • 12,264
  • 11
  • 65
  • 75
Stacie
  • 306
  • 7
  • 26
  • If you want to use `th:field`, you might need to change your controller. See this thread: http://forum.thymeleaf.org/The-checked-attribute-of-the-checkbox-is-not-set-in-th-each-td3043675.html – Gustavo Passini Oct 18 '18 at 01:11
  • Do you know if I don't use th:field (and th:name instead), how I can get th:text to send the value via email ? All of my other form elements are th:field, and on the email helper I have it as th:text to read each input's answer. – Stacie Oct 19 '18 at 00:22
  • Possible duplicate of [Thymeleaf - How to add checked attribute to input conditionally](https://stackoverflow.com/questions/29826576/thymeleaf-how-to-add-checked-attribute-to-input-conditionally) – DimaSan Oct 19 '18 at 23:15

1 Answers1

0

Figured out a way to use th:field and have it default to checked. Have to set it in my controller, like Gustavo mentioned. Code example below.

...
directBind.setDirectBox(true);
directBind.setServiceLevel(true);
model.addAttribute("directBind", directBind);
return "directBind";
DimaSan
  • 12,264
  • 11
  • 65
  • 75
Stacie
  • 306
  • 7
  • 26