Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring validation @AssertTrue

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?

like image 554
Eqbal Avatar asked May 18 '10 00:05

Eqbal


3 Answers

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.

like image 178
Atais Avatar answered Nov 12 '22 23:11

Atais


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.

like image 9
Eqbal Avatar answered Nov 12 '22 23:11

Eqbal


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"/>
like image 2
hunglevn Avatar answered Nov 12 '22 23:11

hunglevn