Przemek Wojtas
Przemek Wojtas

Reputation: 1371

Spring form validation simplify error messages

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!

Upvotes: 0

Views: 956

Answers (3)

Tanmoy Mandal
Tanmoy Mandal

Reputation: 496

@Past(message="You can't be born yet")

hopefully this you are looking for.

Upvotes: 1

Suman Behara
Suman Behara

Reputation: 160

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>

Upvotes: -1

destan
destan

Reputation: 4411

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

Upvotes: 2

Related Questions