0

I am working with my form in spring + thymeleaf and I am wondering if there is a way to simplify an error message for date field as when the value is wrong I get this:

enter image description here

Instead I would like to simply have a text saying: You can't be born yet, or something like that.

Model:

@DateTimeFormat(pattern = "dd.mm.yyyy")
    @Past
    private Date dob;

Form:

Date: <input type="date" th:field="*{dob}" />
<p th:if="${#fields.hasErrors('dob')}" th:errors="*{dob}"></p>

Thanks for any help!

Przemek Wojtas
  • 1,311
  • 5
  • 26
  • 51

3 Answers3

2

First of all your current message is not due to the date being in the future but due to the date format is wrong and the date cannot be parsed.

Other than that you can customize error messages like this: https://stackoverflow.com/a/5781678/878361

Community
  • 1
  • 1
destan
  • 4,301
  • 3
  • 35
  • 62
1
@Past(message="You can't be born yet")

hopefully this you are looking for.

Tanmoy Mandal
  • 466
  • 4
  • 14
-1

Model:

 @Temporal(javax.persistence.TemporalType.TIMESTAMP)
 private Date dob;

Form:

Add in taglib and dob field in your form

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

Add date property

<fmt:formatDate value="${dob}" pattern="dd-MM-yyyy" var="dob"/>
<p th:if="${#fields.hasErrors('dob')}" th:errors="*{dob}"></p>
Suman Behara
  • 150
  • 9