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.

EricSchaefer
  • 25,272
  • 21
  • 67
  • 103
ghilton
  • 81
  • 2
  • 1
    Very frustrating that this isn't handled out of the box. I'm looking at building my own solution :-( – Peter Davis Apr 12 '11 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: http://community.jboss.org/thread/164600 – ghilton Apr 13 '11 at 21:53
  • As you mentioned ExtVal can do it and we are using it for such use-cases successfully. – Dar Whi Aug 30 '11 at 02:36
  • This is already answered here: http://stackoverflow.com/questions/11890334/cross-field-validation-with-hibernatevalidator-works-fine-but-displays-no-error – Zilvinas Feb 18 '14 at 10:06

1 Answers1

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
rogergl
  • 3,501
  • 2
  • 30
  • 49