2

With default BeanValidation annotations like @Future, @NotNull, etc i get the fields in my JSF form highlighted with validation error text shown by default. However when i tried to validate RoomReservation against list of reservations for selected room and selected from/to dates i get the exception printed in the stack trace. I assume the exception is saying that the reservation didnt pass validation rather then the validation process failed and that this exception couldnt get processed by whatever handles ValidationExceptions and got rethrown.

As a workarround i use try catch block in my controller, and display placeholder error message if it gets thrown, but i would like to avoid this.

Is it possible to have JSF process this expception and update the form ?

This is my RoomReservation class:

@Entity
@RoomAvailable
public class RoomReservation{

    //fields, getters, setters follow

}

In order to validate room reservation i implemented class level annotation:

@Constraint(validatedBy = RoomAvailableValidator.class)
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface RoomAvailable{

    String message() default "{invalid.roomReserved}";

    Class<?>[] groups() default {};

    boolean optional() default true;

    Class<? extends Payload>[] payload() default {};

}

This is the isValid method of RoomAvailableValidator:

@Override
public boolean isValid(RoomReservation reservation, ConstraintValidatorContext context) {
    boolean isValid = evaluate(reservation);

    if(!isValid){
        context.disableDefaultConstraintViolation();
        context.buildConstraintViolationWithTemplate( "{invalid.roomReserved}" )
        .addNode( "dateFrom" )
        .addConstraintViolation();
    }

    return isValid;
}

what i expected from this method was to "mark" the dateFrom field as invalid, so it gets highlighted in the form with validation error message. I get this behaviour by default when using field level annotations. However, all i get is this warning.

25.7.2013 18:50:27 com.sun.faces.lifecycle.InvokeApplicationPhase execute
WARNING: #{roomController.saveRoomReservation}:
javax.validation.ConstraintViolationException: Validation failed for classes
[sk.bantip.hotel.server.model.entity.venues.Session] during update time for groups
[javax.validation.groups.Default, ]
List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage='{invalid.roomReserved}',
propertyPath=dateFrom, rootBeanClass=class 
sk.bantip.hotel.server.model.entity.venues.Session, 
messageTemplate='{invalid.venueReserved}'}]

javax.faces.FacesException: #{roomController.saveRoomReservation}:
javax.validation.ConstraintViolationException: Validation failed for classes 
[package.RoomReservation] during update time for groups 
[javax.validation.groups.Default, ]
List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage='{invalid.roomReserved}', 
propertyPath=dateFrom, rootBeanClass=class package.RoomReservation, 
messageTemplate='{invalid.roomReserved}'}]

followed by a stack trace.

pvytykac
  • 144
  • 6

0 Answers0