public class PatternValidator extends Object implements IValidator<String>
IValidatable by matching the value against a regular expression pattern. A
PatternValidator can be constructed with either a Java regular expression (compiled
or not) or a MetaPattern. If the pattern matches against the value then it is considered
valid. If the pattern does not match, the an IValidationError will be reported on the
IValidatable.
For example, to restrict a field to only digits, you might add a PatternValidator
constructed with the pattern "\d+". Another way to do the same thing would be to construct the
PatternValidator passing in MetaPattern.DIGITS. The advantages of using
MetaPattern over straight Java regular expressions are that the patterns are easier to
construct and easier to combine into complex patterns. They are also more readable and more
reusable. See MetaPattern for details.
The error message will be generated with the key "PatternValidator" and one additional message
key ${pattern} for the pattern which failed to match. See FormComponent for a list of
further messages keys.
Pattern,
Serialized Form| Constructor and Description |
|---|
PatternValidator(MetaPattern pattern)
Constructor that accepts a
MetaPattern argument. |
PatternValidator(Pattern pattern)
Constructor that accepts a compiled pattern.
|
PatternValidator(String pattern)
Constructor that accepts a
String regular expression pattern. |
PatternValidator(String pattern,
int flags)
Constructor that accepts a
String pattern and Java regex compile
flags as arguments. |
| Modifier and Type | Method and Description |
|---|---|
protected IValidationError |
decorate(IValidationError error,
IValidatable<String> validatable)
Allows subclasses to decorate reported errors
|
Pattern |
getPattern()
Gets the regexp pattern.
|
PatternValidator |
setReverse(boolean reverse)
If set to true then input that matches the pattern is considered invalid.
|
String |
toString() |
void |
validate(IValidatable<String> validatable)
Checks a value against this
PatternValidator's Pattern. |
public PatternValidator(String pattern)
String regular expression pattern.pattern - a regular expression patternpublic PatternValidator(String pattern, int flags)
String pattern and Java regex compile
flags as arguments.pattern - a regular expression patternflags - compile flags for java.util.regex.Patternpublic PatternValidator(Pattern pattern)
pattern - a compiled patternpublic PatternValidator(MetaPattern pattern)
MetaPattern argument.pattern - a MetaPatternpublic final Pattern getPattern()
public PatternValidator setReverse(boolean reverse)
reverse - public String toString()
toString in class ObjectObject.toString()public void validate(IValidatable<String> validatable)
PatternValidator's Pattern.validate in interface IValidator<String>validatable - the IValidatable to checkprotected IValidationError decorate(IValidationError error, IValidatable<String> validatable)
error - validatable - Copyright © 2006–2014 Apache Software Foundation. All rights reserved.