How do I display on a view jsp validation error message that occurs as a result of @AssertTrue
annotation? It isn't tied to a specific field, but I am using it to validate a combination of fields. If I use <form:errors path="*"/>
that will display all the errors for that form?
Asked
Active
Viewed 3.0k times
12

Taylor Leese
- 51,004
- 28
- 112
- 141

Eqbal
- 4,722
- 12
- 38
- 47
3 Answers
32
From what I have tested it is important HOW you name your test function. And you should name it properly.
You do not need field, getter or setter but your function HAVE TO start with 'is*' statement.
fe.
@AssertTrue
public boolean isConditionTrue() {
...
...
}
or
@AssertTrue
public boolean isSomethingElseOk() {
...
...
}
Though, you need a field and getter/setter if you need to use a error form with path, like:
<form:errors path="someFieldToDisplay" />
But i think this is quite obvious.
Some schema problem which I didn't step into but might be helpful:
This might be helpful as well: lack of error messages.
But if you use schema without version tag, it uses the newest version by default.
-
Renaming the method from valid() to isValid() fixed it for me with Spring using Hibernate Validation. – bernie Mar 31 '16 at 22:04
-
Renaming to is* fixed for me, too. Thanks for the answer. This should be accepted as correct answer. – Michael Hegner Oct 22 '17 at 13:22
-
1This indeed should be the accepted answer. I had no idea about the necessary naming convention – tkralik Dec 19 '17 at 17:05
-
This is crazy. Thank you. – Tobia Dec 30 '20 at 14:48
10
Declaring a boolean property is what seems to work for this. So if there is:
@AssertTrue
public boolean isConditionTrue() {
...
...
}
then declaring a property like:
private boolean conditionTrue;
works.

Eqbal
- 4,722
- 12
- 38
- 47
2
You should name your property like this:
@AssertTrue(message = "....")
private boolean conditionTrue; //***NOT isConditionTrue***
public boolean isConditionTrue() {
return conditionTrue;
}
public void setConditionTrue(boolean conditionTrue) {
this.conditionTrue= conditionTrue;
}
<form:errors path="*"/> or
<form:errors path="conditionTrue"/>

hunglevn
- 65
- 1