Search code examples
javahtmlthymeleaf

thymeleaf html5 datepicker setting min date from variable not working


Im trying to set the an html datepicker to not allow dates before today to be picked. For some reason it wont read it from a stored variable.

in controller:

    LocalDate now = LocalDate.now();
model.addAttribute("now", now);

in html:

<input type="date" name="bookingDate" min="${now}"/>

I outputted "now" to the console while running to see its output and I got 2017-12-11 exactly as the min value of date asks for. It doesnt set the datepicker. I copy pasted the output from console directly into the html instead of ${now} and it worked.
I also formatted it as a string instead of a LocalDate just as a test. Both outputs to console were the same but neither worked in html. Am I missing something simple or is there no way to do this?


Solution

  • You can use th:attr to add a new attribute.

    For example -

    th:attr="min=${now}"
    

    Look here for the more detailed explanation.