8

What's the best way to invoke class level JSR-303 constraints that do cross field validation from JSF and have the resulting messages get translated to a FacesMessage and be tied to a particular JSF component based on the PropertyPath in the ConstraintViolation?

rich:graphValidator is close, but it doesn't make use of the PropertyPath. Perhaps MyFaces extval could get me close, but there seems to be a whole extra layer of framework on time of bean-validation, so I avoided it.

Here's a simple example:

public enum Type {
    ROAD, RACE;
}
    
public class Driver {
    private String name;
    private Type licenseType;
    ...
}
     
@CarConstraint
public class Car {
    @Valid
    private Driver driver;
    private Type carType;
    private String make;
    private String model;
    ...
}

public class CarConstraintValidator implements ConstraintValidator<CarConstraint, Car> {
    @Override
    public void initialize(CarConstraint constraintAnnotation) {}
     
    @Override
    public boolean isValid(Car value, ConstraintValidatorContext context) {
        if (value == null) { return true; }
     
        if (Type.RACE.equals(value.getCarType()) 
            && !Type.RACE.equals(value.getDriver().getLicenseType())) {

            context.buildConstraintViolationWithTemplate("Driver of this car must have a racing license")
                .addNode("driver")
                .addNode("licenseType")
                .addConstraintViolation();
     
            return false;
        }
     
        return true;
    }
}

Picture a form where the information about the car and the driver are input. If the Driver had a license type of ROAD and the Car had a car type of RACE, it'd be ideal to see a resulting validation message be translated into a FacesMessage which is connected to the input for license type, since the message was added to that node using the fluent API of Bean Validation.

4
  • 1
    Very frustrating that this isn't handled out of the box. I'm looking at building my own solution :-( Apr 12, 2011 at 20:10
  • You make me feel better that I'm not the only one looking for this. I was thinking about creating my own component as well, perhaps by taking inspiration from rich:graphValidator and/or using rich faces' CDK. Earlier I started a similar discussion on the RichFaces forum: community.jboss.org/thread/164600
    – ghilton
    Apr 13, 2011 at 21:53
  • As you mentioned ExtVal can do it and we are using it for such use-cases successfully.
    – Dar Whi
    Aug 30, 2011 at 2:36
  • This is already answered here: stackoverflow.com/questions/11890334/…
    – Zilvinas
    Feb 18, 2014 at 10:06

1 Answer 1

0

Our team had the same problem. You may have a look OmniFaces. I used code from the validators as a blueprint to build a prototype that used reflection to call the class level validators.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.