Class PatternValidator
- java.lang.Object
-
- org.apache.wicket.validation.validator.PatternValidator
-
- All Implemented Interfaces:
Serializable
,IClusterable
,IValidator<String>
- Direct Known Subclasses:
EmailAddressValidator
public class PatternValidator extends Object implements IValidator<String>
Validates anIValidatable
by matching the value against a regular expression pattern. APatternValidator
can be constructed with either a Java regular expression (compiled or not) or aMetaPattern
. If the pattern matches against the value then it is considered valid. If the pattern does not match, the anIValidationError
will be reported on theIValidatable
.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 thePatternValidator
passing inMetaPattern.DIGITS
. The advantages of usingMetaPattern
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. SeeMetaPattern
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.- Since:
- 1.2.6
- Author:
- Jonathan Locke, Igor Vaynberg (ivaynberg)
- See Also:
Pattern
, Serialized Form
-
-
Constructor Summary
Constructors Constructor Description PatternValidator(String pattern)
Constructor that accepts aString
regular expression pattern.PatternValidator(String pattern, int flags)
Constructor that accepts aString
pattern and Javaregex
compile flags as arguments.PatternValidator(Pattern pattern)
Constructor that accepts a compiled pattern.PatternValidator(MetaPattern pattern)
Constructor that accepts aMetaPattern
argument.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected IValidationError
decorate(IValidationError error, IValidatable<String> validatable)
Allows subclasses to decorate reported errorsPattern
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 thisPatternValidator
'sPattern
.
-
-
-
Constructor Detail
-
PatternValidator
public PatternValidator(String pattern)
Constructor that accepts aString
regular expression pattern.- Parameters:
pattern
- a regular expression pattern
-
PatternValidator
public PatternValidator(String pattern, int flags)
Constructor that accepts aString
pattern and Javaregex
compile flags as arguments.- Parameters:
pattern
- a regular expression patternflags
- compile flags forjava.util.regex.Pattern
-
PatternValidator
public PatternValidator(Pattern pattern)
Constructor that accepts a compiled pattern.- Parameters:
pattern
- a compiled pattern
-
PatternValidator
public PatternValidator(MetaPattern pattern)
Constructor that accepts aMetaPattern
argument.- Parameters:
pattern
- aMetaPattern
-
-
Method Detail
-
getPattern
public final Pattern getPattern()
Gets the regexp pattern.- Returns:
- the regexp pattern
-
setReverse
public PatternValidator setReverse(boolean reverse)
If set to true then input that matches the pattern is considered invalid.- Parameters:
reverse
-- Returns:
- itself
-
toString
public String toString()
- Overrides:
toString
in classObject
- See Also:
Object.toString()
-
validate
public void validate(IValidatable<String> validatable)
Checks a value against thisPatternValidator
'sPattern
.- Specified by:
validate
in interfaceIValidator<String>
- Parameters:
validatable
- theIValidatable
to check
-
decorate
protected IValidationError decorate(IValidationError error, IValidatable<String> validatable)
Allows subclasses to decorate reported errors- Parameters:
error
-validatable
-- Returns:
- decorated error
-
-