Issue
I'm working on a web application using Spring Boot + Thymeleaf and I'm using @Size
annotation to validate a field of a form backing object:
@Size(max=50)
private String name;
By using the default error message, all works fine and I'm getting the following message:
size must be between 0 and 50
Now I'm trying to customize the error message. First of all I've tried the following:
@Size(max=50, message="Max size: {max}")
private String name;
And it's working correctly:
Max size: 50
But now I'd like to move the custom message in a messages.properties
file. I set the property this way:
Size=Max size: {max}
But now I'm getting this error:
org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringErrorsAttrProcessor' with root cause java.lang.NumberFormatException: For input string: "max"
It seems the max
parameter is null, or something similar...
Why? What am I doing wrong?
Solution
In your messages.properties
set like below.
Size=Max size {1}
Your field will be like below.
@Size(max=50)
private String name;
Answered By - abaghel
Answer Checked By - David Marino (JavaFixing Volunteer)