Search code examples
thymeleafmaterialize

Thymeleaf doesn't select disabled option


I am using Materializecss select form http://materializecss.com/forms.html#select and it requires for correct behavior first option to be disabled and selected. Thymeleaf ignores disabled option despite it is selected. Instead it selects first non-disabled option.

<div class="input-field col s6">
    <select th:field="*{locale}" th:errorclass="invalid">
        <option value="" selected="selected" disabled="disabled">Choose your option</option>
        <option value="cs">Czech</option>
        <option value="en">English</option>
    </select>
    <label>Locale</label>
</div>

Czech is automatically selected, but I want to see Choose your option to be selected instead.


Solution

  • Based on Enigo comment, I have made it working by replacing th:field in select element with name and id. I have also added th:disabled and th:selected to the first option for it to be disabled and selected by default.

    <select name="locale" id="locale" th:errorclass="invalid" required="required">
        <option value="" th:disabled="disabled" th:selected="selected">
            Choose your option
        </option>
        <option value="cs">Czech</option>
        <option value="en">English</option>
    </select>