Class PatternValidator
- java.lang.Object
-
- org.apache.wicket.validation.validator.PatternValidator
-
- All Implemented Interfaces:
java.io.Serializable,org.apache.wicket.util.io.IClusterable,IValidator<java.lang.String>
- Direct Known Subclasses:
EmailAddressValidator
public class PatternValidator extends java.lang.Object implements IValidator<java.lang.String>
Validates anIValidatableby matching the value against a regular expression pattern. APatternValidatorcan 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 anIValidationErrorwill be reported on theIValidatable.For example, to restrict a field to only digits, you might add a
PatternValidatorconstructed with the pattern "\d+". Another way to do the same thing would be to construct thePatternValidatorpassing inMetaPattern.DIGITS. The advantages of usingMetaPatternover 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. SeeMetaPatternfor 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
FormComponentfor 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(java.lang.String pattern)Constructor that accepts aStringregular expression pattern.PatternValidator(java.lang.String pattern, int flags)Constructor that accepts aStringpattern and Javaregexcompile flags as arguments.PatternValidator(java.util.regex.Pattern pattern)Constructor that accepts a compiled pattern.PatternValidator(org.apache.wicket.util.parse.metapattern.MetaPattern pattern)Constructor that accepts aMetaPatternargument.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected IValidationErrordecorate(IValidationError error, IValidatable<java.lang.String> validatable)Allows subclasses to decorate reported errorsjava.util.regex.PatterngetPattern()Gets the regexp pattern.PatternValidatorsetReverse(boolean reverse)If set to true then input that matches the pattern is considered invalid.java.lang.StringtoString()voidvalidate(IValidatable<java.lang.String> validatable)Checks a value against thisPatternValidator'sPattern.
-
-
-
Constructor Detail
-
PatternValidator
public PatternValidator(java.lang.String pattern)
Constructor that accepts aStringregular expression pattern.- Parameters:
pattern- a regular expression pattern
-
PatternValidator
public PatternValidator(java.lang.String pattern, int flags)Constructor that accepts aStringpattern and Javaregexcompile flags as arguments.- Parameters:
pattern- a regular expression patternflags- compile flags forjava.util.regex.Pattern
-
PatternValidator
public PatternValidator(java.util.regex.Pattern pattern)
Constructor that accepts a compiled pattern.- Parameters:
pattern- a compiled pattern
-
PatternValidator
public PatternValidator(org.apache.wicket.util.parse.metapattern.MetaPattern pattern)
Constructor that accepts aMetaPatternargument.- Parameters:
pattern- aMetaPattern
-
-
Method Detail
-
getPattern
public final java.util.regex.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 java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object- See Also:
Object.toString()
-
validate
public void validate(IValidatable<java.lang.String> validatable)
Checks a value against thisPatternValidator'sPattern.- Specified by:
validatein interfaceIValidator<java.lang.String>- Parameters:
validatable- theIValidatableto check
-
decorate
protected IValidationError decorate(IValidationError error, IValidatable<java.lang.String> validatable)
Allows subclasses to decorate reported errors- Parameters:
error-validatable-- Returns:
- decorated error
-
-