Search code examples
hibernatevalidationjsfrichfaces

Hibernate validation @Pattern


I am using org.hibernate.validator.Pattern annotation in my jsf managed bean to validate an <h:inputText> component.

@Pattern(regex="\\W+")
public String getText() {
  return text;
}

My question is, is there a way to get the regular expression from a method or EL withought hard-coding it.
For an example

@Pattern(regex = getRexEx())

OR

@Pattern(regex = "#{bean.regEx}")

I googled and found that the regEx should be a constant. However there can be a alternative way to accomplish this.


Solution

  • The arguments to an annotation need to be compile time constants, so no, it's not possible to specify a run time expression as the argument for @Pattern. It's also not possible to pass an EL expression as argument and have it do anything because, well, that's simply not how the pattern validator was written.

    What you can do however, is define your own a validator class that takes a form of an EL expression and validates the bean property against it. Good luck with the context management for that though! Not a simple task.