调用JSF交叉字段验证的类级JSR-303约束的最佳方法是什么,并将结果消息转换为FacesMessage并绑定到基于ConstraintViolation中的PropertyPath的特定JSF组件?
rich:graphValidator很接近,但它没有使用PropertyPath.也许MyFaces的延期可以让我接近,但是在bean验证的时候似乎有一整套额外的框架,所以我避免了它.
这是一个简单的例子:
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 …Run Code Online (Sandbox Code Playgroud)