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 an 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.

Since:
1.2.6
Author:
Jonathan Locke, Igor Vaynberg (ivaynberg)
See Also:
  • Constructor Details

    • PatternValidator

      public PatternValidator(String pattern)
      Constructor that accepts a String regular expression pattern.
      Parameters:
      pattern - a regular expression pattern
    • PatternValidator

      public PatternValidator(String pattern, int flags)
      Constructor that accepts a String pattern and Java regex compile flags as arguments.
      Parameters:
      pattern - a regular expression pattern
      flags - compile flags for java.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 a MetaPattern argument.
      Parameters:
      pattern - a MetaPattern
  • Method Details