Thymeleaf - How to add checked attribute to input conditionally
Asked Answered
K

8

36

As you know, input component has an attribute, checked to whether mark the checkbox as enabled by default or not.

<input type="checkbox" name="mycheckbox" checked="checked"/>

To disable the checkbox by default, the checked exception should be declared. Is it possible to set checked attribute by a flag in Thymeleaf?

Kusin answered 23/4, 2015 at 14:32 Comment(0)
D
72

According to the official thymeleaf documentation

http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#fixed-value-boolean-attributes

th:checked is considered as a fixed-value Boolean attribute.

<input type="checkbox" name="active" th:checked="${user.active}" />

Where user.active should be a boolean.

So in your case it should be as Andrea mentioned,

<input type="checkbox" name="mycheckbox" th:checked="${flag}" />
Disservice answered 28/4, 2015 at 16:45 Comment(1)
Does checkbox not work with th:field attribute? I am adding th:field and everytime the backend receives the value as false?Augsburg
K
8

After digging a little, I found out the solution. There is th:checked attribute for that purpose.

This works:

<input type="checkbox" name="mycheckbox" th:checked="${flag} ? 'checked'">

This fails:

<input type="checkbox" name="mycheckbox" th:checked="${flag} ? 'checked' : ''">

If checked="" is set to input component, it's marked checked. This method is valid for custom attributesth:attr also. Consider following example:

<p th:attr="customattr=${flag}?'attr'></p>

If flag is true, it's replaced with:

<p customattr="attr"></p>

If flag is false, it's replaced with:

<p></p>
Kusin answered 23/4, 2015 at 14:32 Comment(4)
simply th:checked=${flag}, is more concise for checked attributeDrysalt
@Drysalt is it possible to do a comparison i.e. th:checked="${model.myField == null}" ?Layman
how to i set flag as true?Transpolar
Flag is set in request handler, in a class like `UserController`` @JesseKusin
Y
2

You can conditionally add checked attribute to radio input in thymeleaf as below:

 <input type="radio" th:checked="${sales.sales_head.sales_type} == CREDIT" class="sales_type" value="CREDIT"  name="sales_type" >

Here if sales_type is CREDIT the radio will be checked. Otherwise it remains unchecked.

Yate answered 22/11, 2018 at 5:40 Comment(0)
K
2

Thymeleaf th:checked attribute is fixed-value attribute and it does not toggle the boolean value on checking or unchecking the box.

To toggle the boolean value for backend use, use 'th:field attribute with the syntax: th:field="${object.attribute}"

A working code snippet that i used in my project is given here as exmple:

<form action="#" th:action="@{/editNotificationAlerts}" th:object="${userObject}" method="post">

      <label for="sendEmailNotification">Notify At 80</label>
      <input type="checkbox" name="sendEmailNotification" id="sendEmailNotification" th:field="${userObject.sendEmailNotification}">

<div>
     <input type="submit" value="Submit"/>
</div>

</form>

The toggled value shall be passed to controller on submit action.

Knifeedged answered 19/5, 2022 at 7:39 Comment(0)
S
0

Neither suggested solutions worked for me.

This one worked:

th:checked="${#strings.equals(param.myRequestParameterXYZ, 'foobar')}"
Sirdar answered 13/12, 2018 at 11:26 Comment(0)
S
0

I faced problem for showing checkbox (tick mark on/off) in thymeleaf based on true or false value for the property. I solved it by following way.

Method in controller

@RequestMapping(value = "/test")
public String showCheckbox(Model model) {
 boolean myBooleanVariable = false;
 model.addAttribute("myBooleanVariable", myBooleanVariable);
 return "sample-checkbox";
}

In HTML page:

<input type="checkbox" name="myBooleanVariable" th:checked="${myBooleanVariable}"/>
Speculum answered 13/9, 2019 at 2:29 Comment(0)
G
0

If you have a form that is a model representation, say foo, and bar is a checkbox on your form and property of foo (e.g. foo.bar):

<form th:object="${foo}">
    <input type="checkbox" th:field="*{bar}" th:value="*{bar}"></input>
</form>

...will also work and presents a succinct approach.

Gilberto answered 21/9, 2021 at 17:0 Comment(0)
C
0

You can also use Logical and / or conditions with it like

th:checked="${business?.status == true or business?.status == null}"
Caddie answered 14/4, 2023 at 9:18 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.