Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

th:field is braking checked method in html input field

I have a basic HTML post form like this:

<ul>
    <li>
        <input type="radio" th:field="*{type}" th:value="'Flight'" name="flight" id="flight" checked>
        <label for="flight" th:text="'Flight'"/>
    </li>
    <li>
        <input type="radio" th:field="*{type}" th:value="'Bus'" name="bus" id="bus">
        <label for="bus" th:text="'Bus'"/>
    </li>
    <li>
        <input type="radio" th:field="*{type}" th:value="'Personal'" name="personal" id="personal">
        <label for="personal" th:text="'Personal'"/>
    </li>
</ul>

I am trying to take input from radio buttons but checked method is not working:

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

But when I remove the th:field method, it works fine:

enter image description here

What am I doing wrong in here?

>Solution :

I’m pretty sure th:field overwrites the checked attribute. To get a default radio to be checked there are 2 solutions I believe.

  1. pre-fill your th:object bean with the default value. When this is the case th:field should automatically check the correct radio.
  2. Use javascript to check the radio button after rendering

P.S. when setting static string as value you can just use value="Bus" no need to write th:value="'Bus'"

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading