tags:

views:

49

answers:

1

I am using the MyFaces 1.1 ValidateRegExpr tag to validate the input from a user against a regular expression.

I would like to be able to dynamically assign the regex pattern via code, but according to http://myfaces.apache.org/commons11/myfaces-validators11/tagdoc/mcv_validateRegExpr.html the attribute pattern does not support Expression Language.

The markup in my JSP file is supposed to look like this:

<tc:in value="${dataBean.currentBean.field}">
  <mcv:validateRegExpr
   pattern="${dataBean.currentBean.validationRegEx}"
   message="${dataBean.currentBean.validationMessage" />
</tc:in>

What is the preferred way of dynamically assigning attribute values to jsp controls that do not support EL?

+1  A: 

Implement a javax.faces.validator.Validator and register it as <validator> in faces-config.xml. In a real Java class you have the freedom to write normal Java code :)

Basic kickoff example:

public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
    SomeBean someBean = SomeFacesUtil.evaluateExpressionGet("someBean", SomeBean.class);
    if (!value.toString().matches(someBean.getPattern())) {
        throw new ValidatorException(new FacesMessage(someBean.getMessage()));
    }
}
BalusC