JSF 1.2动态添加自定义验证器

时间:2012-05-08 14:45:58

标签: jsf lifecycle jsf-1.2 customvalidator

如果我正确理解JSF生命周期,它会在Apply Request阶段注册Validators。 这是否意味着我不能将addValidator调用到我在我的decode()方法中的Component对象句柄中,该方法在Process Request Events阶段被调用?如果是这样,是否有其他方法可以根据组件的属性值动态添加自定义验证器?

由于

1 个答案:

答案 0 :(得分:0)

我希望应该起作用的是......

public class ValidatorWrapper implements Validator {

private DoubleRangeValidator dbRangeValidator;
private LongRangeValidator lRangeValidator;
private String requiredValidatorType;/*An attribute to choose the type of validator*/

public ValidatorWrapper(){
    dbRangeValidator = new DoubleRangeValidator(10.01, 20.99);lRangeValidator = new LongRangeValidator(10, 20);
}

@Override
public void validate(FacesContext context, UIComponent component,
        Object value) throws ValidatorException {
    if("LONG".equalsIgnoreCase(requiredValidatorType))
        lRangeValidator.validate(context, component, value);
    else if("DBL".equalsIgnoreCase(requiredValidatorType))
        dbRangeValidator.validate(context, component, value);
}  }