Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf: disabling a component

Tags:

html

thymeleaf

I have a basic SpringBoot 2.0.5.RELEASE app. Using Spring Initializer, JPA, embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file. I have this template, where I want to disable a select object based on a condition

<form id="menuFormId" class="form-style-9" action="#" th:action="@{/menu/save}" th:object="${menu}" method="post">
    <ul>
        <li th:each="e : ${#fields.detailedErrors()}" th:class="${e.global}? globalerr : fielderr">
            <span th:text="${e.global}? '*' : ${e.fieldName}" ><b>The field name</b></span> : <span th:text="${e.message}" class="red">
                <font color="red">The error message</font>
            </span>
        </li>
    </ul>
    <ul class="tab_form">
        <li>        
            <select id="selectMenuId" th:field="*{resto}" th:classappend="${menu.id == null} ?  disabled='disabled'">
                <option value="0">PLEASE SELECT A MENU</option>
            </select>
...

But I got this error:

Could not parse as expression: "${menu.id == null} ?  disabled='disabled'"
like image 926
Nunyet de Can Calçada Avatar asked Sep 04 '26 04:09

Nunyet de Can Calçada


1 Answers

Uhhhh, there are 2 issues:

1.) disabled is a attribute and not a class. Therefore, use the following snippet:

th:disabled="${menu.id == null}"

2.) You can't define a class disabled='disabled'.

The second issue isn't important. There is no need for such a class definition.

like image 177
Flocke Avatar answered Sep 06 '26 18:09

Flocke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!