LocalDate in form
I'm unable to reproduce the exact error, but I believe that adding a custom editor for the LocalDate class should fix this. Add this method to your controller:
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(LocalDate.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException{
setValue(LocalDate.parse(text, DateTimeFormatter.ofPattern("yyyy-MM-dd")));
}
@Override
public String getAsText() throws IllegalArgumentException {
return DateTimeFormatter.ofPattern("yyyy-MM-dd").format((LocalDate) getValue());
}
});
}
It's also possible to add this globally, you'll have to create a ControllerAdvice
class and add the method there instead.
Thymeleaf provides an extra module for that: https://github.com/thymeleaf/thymeleaf-extras-java8time
Adding the following dependency (maven) should be enough:
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
Problem solved.. I don't know why but changing my template to:
<input type="date" th:value="*{date}" th:field="*{date}" />
and adding @DateTimeFormat(pattern = "yyyy-MM-dd")
to entity field solved the problem.
import org.springframework.format.annotation.DateTimeFormat;
add the following annotation above date.
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate date;
put additional curly bracket around date. It will convert date into string
<form action="#" th:action="@{/games/addForm}" th:object="${gameForm}" method="post">
<p>Date: <input type="date" th:field="*{{date}}" /></p>
</form>