1

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.

prageeth
  • 7,159
  • 7
  • 44
  • 72
  • 1
    Why do need this do you want to change it at run time? And I agree with the below answer. – MohanaRao SV Dec 01 '12 at 12:44
  • Sure, I also think that the below answer is correct, logical and descriptive. But I am still waiting to find whether there is any other approach.:) – prageeth Dec 01 '12 at 12:47
  • See also http://stackoverflow.com/questions/8994864/how-would-i-specify-a-hibernate-pattern-annotation-using-a-regular-expression for define constraints at runtime. – Grigory Kislin Mar 26 '15 at 22:32

1 Answers1

4

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.

Perception
  • 79,279
  • 19
  • 185
  • 195
  • Thanks for your valuable answer. It seems your suggestion is the only solution for that. But you know I am not going to test it. :) – prageeth Dec 02 '12 at 13:46