0

If I understand the JSF lifecycle correctly, it registers the Validators during Apply Request phase. Does that mean I cannot call addValidator to the Component object handle I have inside my decode() method that gets called during Process Request Events phase? If so, is there any other way of dynamically adding custom Validators based on component's attribute value?

Thanks

phewataal
  • 1,107
  • 4
  • 12
  • 23
  • You can try creating a custom validator decorating the validators you need.Based on an attribute you can select the validator and run it on the component. – Ahamed Mustafa M May 09 '12 at 10:05

1 Answers1

0

What I hope should work is similar to..

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);
}  }
Ahamed Mustafa M
  • 3,069
  • 1
  • 24
  • 34
  • Just curious how do you populate the variable requiredValidatorType? – phewataal May 09 '12 at 13:01
  • Refer to the links (http://www.ibm.com/developerworks/java/library/j-jsf3/) or (http://docs.oracle.com/javaee/5/tutorial/doc/bnauw.html) – Ahamed Mustafa M May 10 '12 at 04:27
  • I will mark it as answer for the abstraction of Validation that you helped me with. I was more curious on how we initialize ValidatorWrapper within the component. I was trying to do something like myComponent.addValidator(ValidatorWrapper); – phewataal May 11 '12 at 15:11