0

I am implementing form validation and running into problems with BigInteger validation in thymeleaf's error messages. Here is my property annotation:

@Digits(integer = 9, fraction = 0, message="Must be a positive integer")
private BigInteger myInteger;

The Controller:

@PostMapping("/")
    public String whatever(@Valid @ModelAttribute Entity myEntity, BindingResult result) {
    
        if (result.hasErrors()) {
            return "index";
        }
        //TODO
        return "index"; 
}

And finally, the HTML code

<span th:if="${#fields.hasErrors('myEntity.myInteger')}"
th:errors="*{myEntity.myInteger}"></span>

Now, this is working fine for my other BigDecimal variables, but BigInteger causes Thymeleaf to display a NumberFormatException instead of my custom message "Must be a positive integer.", presumably because of some priority in error-handling that I am unfamiliar with. I have tried looking for answers but most of them direct me to some messages.properties based solution, which is not present in my project folder. What do I need to do to ensure my custom message is displayed instead of the NumberFormatException?

4
  • show your request i.e Entity Commented Oct 17, 2020 at 18:24
  • You can just create messages.properties in resources folder and add typeMismatch.table.myInteger = Must be a positive integer . Where table is your table name in lower case. Commented Oct 17, 2020 at 20:54
  • @Seldo97 Thank you so much! I had tried this before but it didn't work because I didn't type it in lower case, thank you for specifically pointing it out Commented Oct 17, 2020 at 21:31
  • @TypoTC I added the answer, you can mark it as solution of your problem. Commented Oct 17, 2020 at 21:56

1 Answer 1

1

You can just create messages.properties in resources folder and add typeMismatch.table.myInteger = Must be a positive integer . Where "table" is your entity name in lower case.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.