wicket.markup.html.form.validation
Class PatternValidator

java.lang.Object
  extended by wicket.markup.html.form.validation.AbstractValidator
      extended by wicket.markup.html.form.validation.StringValidator
          extended by wicket.markup.html.form.validation.PatternValidator
All Implemented Interfaces:
java.io.Serializable, IValidator
Direct Known Subclasses:
EmailAddressPatternValidator

public class PatternValidator
extends StringValidator

Validates component by matching the component's 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 of the component it is attached to when validate() is called by the framework, then that input value is considered valid. If the pattern does not match, the errorMessage() method will be called.

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 MetaPatternfor details.

The error message will be generated with the key "PatternValidator" and the messages keys that can be used are:

Author:
Jonathan Locke
See Also:
Pattern, MetaPattern, Serialized Form

Constructor Summary
PatternValidator(MetaPattern pattern)
          Constructor.
PatternValidator(java.util.regex.Pattern pattern)
          Constructor.
PatternValidator(java.lang.String pattern)
          Constructor.
PatternValidator(java.lang.String pattern, int flags)
          Constructor.
 
Method Summary
 java.util.regex.Pattern getPattern()
          Gets the regexp pattern.
protected  java.util.Map messageModel(FormComponent formComponent)
          Gets the default variables for interpolation.
 void onValidate(FormComponent formComponent, java.lang.String value)
          Validates the set pattern.
protected  java.lang.String resourceKey(FormComponent formComponent)
          Gets the resource key for validator's error message from the ApplicationSettings class.
 java.lang.String toString()
           
 
Methods inherited from class wicket.markup.html.form.validation.StringValidator
exactLength, lengthBetween, maximumLength, minimumLength, validate
 
Methods inherited from class wicket.markup.html.form.validation.AbstractValidator
error, error, error, error
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

PatternValidator

public PatternValidator(java.lang.String pattern)
Constructor.

Parameters:
pattern - Regular expression pattern

PatternValidator

public PatternValidator(java.lang.String pattern,
                        int flags)
Constructor.

Parameters:
pattern - Regular expression pattern
flags - Compile flags for java.util.regex.Pattern

PatternValidator

public PatternValidator(java.util.regex.Pattern pattern)
Constructor.

Parameters:
pattern - Java regex pattern

PatternValidator

public PatternValidator(MetaPattern pattern)
Constructor.

Parameters:
pattern - MetaPattern pattern
Method Detail

onValidate

public void onValidate(FormComponent formComponent,
                       java.lang.String value)
Validates the set pattern.

Specified by:
onValidate in class StringValidator
Parameters:
formComponent - form component
value - The string value to validate
See Also:
StringValidator.onValidate(wicket.markup.html.form.FormComponent,String)

getPattern

public final java.util.regex.Pattern getPattern()
Gets the regexp pattern.

Returns:
the regexp pattern

messageModel

protected java.util.Map messageModel(FormComponent formComponent)
Description copied from class: AbstractValidator
Gets the default variables for interpolation. These are:

Overrides:
messageModel in class AbstractValidator
Parameters:
formComponent - form component
Returns:
a map with the variables for interpolation

resourceKey

protected java.lang.String resourceKey(FormComponent formComponent)
Description copied from class: AbstractValidator
Gets the resource key for validator's error message from the ApplicationSettings class.

Overrides:
resourceKey in class AbstractValidator
Parameters:
formComponent - form component that is being validated
Returns:
the resource key based on the form component
See Also:
AbstractValidator.resourceKey(wicket.markup.html.form.FormComponent)

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object
See Also:
Object.toString()


Copyright © 2004-2007 Wicket developers. All Rights Reserved.