Jose
Jose

Reputation: 21

Problem assigning an initial value to an input date in Thymeleaf

I am trying to pass an initial value to an input date control in a thymeleaf template. In my entity, the value is a LocalDate value, and in the HTML template I have the following code:

<input type="date"
       th:field="*{offerEndDate}"
       th:value="${#temporals.format(sellerProduct.offerEndDate, 'yyyy-MM-dd')}"
       required disabled>

The problem is that, in my browser (I have tried both Chrome and Firefox) the format changes to my local date format:

 <input type="date"
               min="2025-02-13"
               value="17/3/25"
               onchange="onStartDateChange()"
               required id="offerStartDate" name="offerStartDate">

I have also tried to make the conversion in the controller and send a String (correctly formatted) to the thymeleaf template (using the Model parameter) but it does exactly the same (the string format is changed to my local date format). Due to this change, the value is not set correctly.

I would appreciate any ideas to solve this issue.

Upvotes: 0

Views: 29

Answers (1)

jonghoonpark
jonghoonpark

Reputation: 31

I think it's because you're using th:field and th:value together.

You may have two options.

  1. Remove th:value and use th:field
    in this case, change the date format from the controller and then pass the value.

  2. Remove th:field and use th:value
    in this case, make sure to add the id and name attributes in thymeleaf template (html).

Upvotes: 1

Related Questions