Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to pass variables to a validation constraint message in Java?

I have a requirement where in a validation constraint message I need to include the String.length() i.e.:

@max(value = 50, message = "Max length 50, observed length " + String.length())

However I can't do this because it needs to be passed a constant expression. Is there any simple way to deal with this or is it just best to create a separate method checking lengths?

like image 860
Finnegan Avatar asked Nov 21 '25 20:11

Finnegan


1 Answers

In the end we did it with an EL expression:

@max(value = 50, message = "Max length 50, observed length ${String.length()}")
like image 102
Finnegan Avatar answered Nov 23 '25 09:11

Finnegan