views:

606

answers:

4

How do we enforce cross field validation with hibernate validator 3.1.0.GA

create table user (id, start_date, end_date, ...) e.g. college graduation finishing date for a student should be greater than the graduation start date

How do we enforce this, so that the validation messages can be shown in the UI on save / update operations. The UI is built using JSF, Richfaces

+1  A: 

You do this by creating a custom validator. There's more information in the documentation.

Jamie Ide
+1  A: 

Try this:

public class User {

  private Date startDate = null;
  private Date endDate = null;

  @SuppressWarnings("unused")
  @AssertTrue(message="college graduation finishing date for a student should be greater than the graduation start date")
  private boolean dateValidation() {
    return this.startDate < this.endDate;
  }
}
Alberthoven
The user interface is built using Seam / richfaces and form validation is actually tied to the field level validation. Hence, I would expect this dateValidation() method call to be called by some piece of code in the @Entity class and would like the message to be associated with the endDate member variable, so whenever the endDate is lesser than the startDate the message should be shown as a validation message on the endDate field on a form submit.In the above snippet the field level association is missing, I am not sure how to do that
Joshua
The above validation does get triggered, but currently it is causing the user interface to crash. It is not clear on how to associate this with the appropriate form variable in the code so that Seam can display this message.
Joshua
A: 

See my duplicate question (and answer) in http://stackoverflow.com/questions/1972933/cross-field-validation-with-hibernate-validator-jsr-303

In short, create a custom class level validator that associates the class level ConstraintViolation to the specific fields being validated.

bradhouse
I just realised I missed 3.1.0GA in the title. It's possible that my solution does not work under 3.1.0 as there were some changes in this area for 4.0. If I get to it (before somebody else), I'll check if this is the case and remove my answer if necessary.
bradhouse
A: 

for this approach

@SuppressWarnings("unused")

@AssertTrue(message="college graduation finishing date for a student should be greater than the graduation start date") private boolean dateValidation() { return this.startDate < this.endDate; }

to be valid, it would need to conform to the standard bean specification, which means either a get/set/ or "is" prefix. isDateValid() would work-

chrismarx
I am not sure if confirming to the bean convention is the issue here. The validations were being raised but the Jboss Seam framework wasn't able to trap class level validations and associate it with a bean parameter in the user interface. This issue still persists.
Joshua
The workaround is to manually add the class level validations to the EntityHome.update / persist methods
Joshua