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.

3
  • 1
    Why do need this do you want to change it at run time? And I agree with the below answer. Commented Dec 1, 2012 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.:) Commented Dec 1, 2012 at 12:47
  • See also stackoverflow.com/questions/8994864/… for define constraints at runtime. Commented Mar 26, 2015 at 22:32

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.