org.apache.click.extras.control
Class RegexField

java.lang.Object
  extended by org.apache.click.control.AbstractControl
      extended by org.apache.click.control.Field
          extended by org.apache.click.control.TextField
              extended by org.apache.click.extras.control.RegexField
All Implemented Interfaces:
Serializable, Control, Stateful

public class RegexField
extends TextField

Provides a Regex Field control:   <input type='text'>.

Regex Field
RegexField will validate the value using regular expression when the control is processed and invoke the control listener if defined.

RegexField Example

Examples using RegexField for version number and URL input are provided below:
 RegexField versionField = new RegexField("version");
 versionField.setPattern("[0-9]+\\.[0-9]+\\.[0-9]+");
 form.add(versionField);

 RegexField urlField = new RegexField("url", "URL");
 urlField.setPattern("(http|https)://.+");
 form.add(urlField); 
For details on valid regular expression patterns see Pattern Javadoc.

Note for performance reasons the regular expression pattern is compiled when the field is processed not when its value is set. If you set an invalid expression pattern a PatternSyntaxException will be thrown by the Field.onProcess() method.

CSS and JavaScript resources

The RegexField control makes use of the following resources (which Click automatically deploys to the application directory, /click): To import these RegexField files simply reference the variables $headElements and $jsElements in the page template.

See also the W3C HTML reference: INPUT

See Also:
Serialized Form

Field Summary
protected  String pattern
          The field pattern based on regular expression.
protected static String VALIDATE_REGEXFIELD_FUNCTION
          The field validation JavaScript function template.
 
Fields inherited from class org.apache.click.control.TextField
maxLength, minLength, size, VALIDATE_TEXTFIELD_FUNCTION
 
Fields inherited from class org.apache.click.control.Field
disabled, error, focus, form, help, label, labelStyle, labelStyleClass, parentStyleClassHint, parentStyleHint, readonly, required, tabindex, title, trim, validate, value
 
Fields inherited from class org.apache.click.control.AbstractControl
actionListener, attributes, behaviors, headElements, listener, listenerMethod, messages, name, parent, styles
 
Fields inherited from interface org.apache.click.Control
CONTROL_MESSAGES
 
Constructor Summary
RegexField()
          Create a RegexField with no name defined.
RegexField(String name)
          Construct the RegexField with the given name.
RegexField(String name, boolean required)
          Construct the RegexField with the given name and required status.
RegexField(String name, String label)
          Construct the RegexField with the given name and label.
RegexField(String name, String label, boolean required)
          Construct the RegexField with the given name, label and required status.
RegexField(String name, String label, int size)
          Construct the RegexField with the given name, label and size.
RegexField(String name, String label, int size, boolean required)
          Construct the RegexField with the given name, label, size and required status.
 
Method Summary
 List<Element> getHeadElements()
          Returns the RegexField HTML HEAD elements for the click/extras-control.js resource.
 String getPattern()
          Returns the field pattern.
 String getValidationJavaScript()
          Return the field JavaScript client side validation function.
 void setPattern(String pattern)
          Sets the field pattern as regular expression.
 void validate()
          Validate the RegexField request submission.
 
Methods inherited from class org.apache.click.control.TextField
getControlSizeEst, getMaxLength, getMinLength, getSize, getTag, getType, render, setMaxLength, setMinLength, setSize
 
Methods inherited from class org.apache.click.control.Field
bindRequestValue, getError, getErrorLabel, getFocus, getFocusJavaScript, getForm, getHelp, getId, getLabel, getLabelStyle, getLabelStyleClass, getParentStyleClassHint, getParentStyleHint, getRequestValue, getState, getTabIndex, getTextAlign, getTitle, getValidate, getValue, getValueObject, getWidth, isDisabled, isHidden, isReadonly, isRequired, isTrim, isValid, onProcess, removeState, renderTagBegin, restoreState, saveState, setDisabled, setError, setErrorMessage, setErrorMessage, setFocus, setForm, setHelp, setLabel, setLabelStyle, setLabelStyleClass, setListener, setParent, setParentStyleClassHint, setParentStyleHint, setReadonly, setRequired, setState, setTabIndex, setTextAlign, setTitle, setTrim, setValidate, setValue, setValueObject, setWidth
 
Methods inherited from class org.apache.click.control.AbstractControl
addBehavior, addStyleClass, appendAttributes, dispatchActionEvent, getActionListener, getAttribute, getAttributes, getBehaviors, getContext, getHtmlImports, getMessage, getMessage, getMessages, getName, getPage, getParent, getStyle, getStyles, hasAttribute, hasAttributes, hasBehaviors, hasStyles, isAjaxTarget, onDeploy, onDestroy, onInit, onRender, removeBehavior, removeStyleClass, renderTagEnd, setActionListener, setAttribute, setId, setName, setStyle, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

VALIDATE_REGEXFIELD_FUNCTION

protected static final String VALIDATE_REGEXFIELD_FUNCTION
The field validation JavaScript function template. The function template arguments are:

See Also:
Constant Field Values

pattern

protected String pattern
The field pattern based on regular expression. If the pattern is specified, RegexField validates the field value using this.

Constructor Detail

RegexField

public RegexField(String name)
Construct the RegexField with the given name.

Parameters:
name - the name of the field

RegexField

public RegexField(String name,
                  boolean required)
Construct the RegexField with the given name and required status.

Parameters:
name - the name of the field
required - the field required status

RegexField

public RegexField(String name,
                  String label,
                  boolean required)
Construct the RegexField with the given name, label and required status.

Parameters:
name - the name of the field
label - the label of the field
required - the field required status

RegexField

public RegexField(String name,
                  String label,
                  int size)
Construct the RegexField with the given name, label and size.

Parameters:
name - the name of the field
label - the label of the field
size - the size of the field

RegexField

public RegexField(String name,
                  String label,
                  int size,
                  boolean required)
Construct the RegexField with the given name, label, size and required status.

Parameters:
name - the name of the field
label - the label of the field
size - the size of the field
required - the field required status

RegexField

public RegexField(String name,
                  String label)
Construct the RegexField with the given name and label.

Parameters:
name - the name of the field
label - the label of the field

RegexField

public RegexField()
Create a RegexField with no name defined.

Please note the control's name must be defined before it is valid.

Method Detail

setPattern

public void setPattern(String pattern)
Sets the field pattern as regular expression.

Parameters:
pattern - the field regular expression pattern
Throws:
PatternSyntaxException - if the regular expression pattern cannot be compiled

getPattern

public String getPattern()
Returns the field pattern.

Returns:
the field pattern

getHeadElements

public List<Element> getHeadElements()
Returns the RegexField HTML HEAD elements for the click/extras-control.js resource.

Specified by:
getHeadElements in interface Control
Overrides:
getHeadElements in class AbstractControl
Returns:
the HTML HEAD elements for the control
See Also:
Control.getHeadElements()

getValidationJavaScript

public String getValidationJavaScript()
Return the field JavaScript client side validation function.

The function name must follow the format validate_[id], where the id is the DOM element id of the fields focusable HTML element, to ensure the function has a unique name.

Overrides:
getValidationJavaScript in class TextField
Returns:
the field JavaScript client side validation function

validate

public void validate()
Validate the RegexField request submission.

A field error message is displayed if a validation error occurs. These messages are defined in the resource bundle:

  • /click-control.properties
    • field-maxlength-error
    • field-minlength-error
    • field-required-error
  • /org/apache/click/extras/control/RegexField.properties
    • field-pattern-error

Overrides:
validate in class TextField
Throws:
PatternSyntaxException - if the pattern has a syntax error