org.zkoss.zul
Class SimpleConstraint

java.lang.Object
  extended by org.zkoss.zul.SimpleConstraint
All Implemented Interfaces:
java.io.Serializable, ClientConstraint, Constraint
Direct Known Subclasses:
SimpleDateConstraint, SimpleDoubleSpinnerConstraint, SimpleSpinnerConstraint

public class SimpleConstraint
extends java.lang.Object
implements Constraint, ClientConstraint, java.io.Serializable

The default constraint supporting no empty, regular expressions and so on.

Depending on the component (such as Intbox and Datebox, you could combine the flags, such as NO_POSITIVE + NO_ZERO to accept only negative number.

Author:
tomyeh
See Also:
Serialized Form

Field Summary
protected  int _flags
          The constraints.
static int AFTER_END
          The Error-box position.
static int AFTER_POINTER
          The Error-box position.
static int AFTER_START
          The Error-box position.
static int AT_POINTER
          The Error-box position.
static int BEFORE_END
          The Error-box position.
static int BEFORE_START
          The Error-box position.
static int END_AFTER
          The Error-box position.
static int END_BEFORE
          The Error-box position.
static int NO_EMPTY
          Empty is not allowed.
static int NO_FUTURE
          Date in the future is not allowed.
static int NO_NEGATIVE
          Negative numbers are not allowed.
static int NO_PAST
          Date in the past is not allowed.
static int NO_POSITIVE
          Positive numbers are not allowed.
static int NO_TODAY
          Today is not allowed.
static int NO_ZERO
          Zero numbers are not allowed.
static int OVERLAP
          The Error-box position.
static int OVERLAP_AFTER
          The Error-box position.
static int OVERLAP_BEFORE
          The Error-box position.
static int OVERLAP_END
          The Error-box position.
static int SERVER
          Indicates this constraint requires the server validation.
static int START_AFTER
          The Error-box position.
static int START_BEFORE
          The Error-box position.
static int STRICT
          The value must match inside the data from ListModel only.
 
Constructor Summary
SimpleConstraint(int flags)
          Constructs a constraint with flags.
SimpleConstraint(int flags, java.lang.String errmsg)
          Constructs a constraint with flags and an error message.
SimpleConstraint(int flags, java.lang.String regex, java.lang.String errmsg)
          Constructs a constraint combining regular expression.
SimpleConstraint(java.lang.String constraint)
          Constructs a constraint with a list of constraints separated by comma.
SimpleConstraint(java.lang.String regex, java.lang.String errmsg)
          Constructs a regular-expression constraint.
 
Method Summary
 java.lang.String getClientConstraint()
          Returns the JavaScript snippet that will be evaluated at client to return a validator, or null if no client constraint is supported.
 java.lang.String getClientPackages()
          Default: null (since it depends on zul.inp which is loaded for all input widgets).
 java.lang.String getErrorMessage(Component comp)
          Returns the custom error message that shall be shown if an error occurs, or null if no custom error message specified.
 int getFlags()
          Returns the constraint flags, i.e., a combination of NO_POSITIVE, NO_NEGATIVE, STRICT and others.
static SimpleConstraint getInstance(java.lang.String constraint)
          Parses a list of constraints from a string to an integer representing a combination of NO_POSITIVE and other flags.
protected  int parseConstraint(java.lang.String constraint)
          Parses a constraint into an integer value.
 void validate(Component comp, java.lang.Object value)
          Verifies whether the value is acceptable.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NO_POSITIVE

public static final int NO_POSITIVE
Positive numbers are not allowed.

See Also:
Constant Field Values

NO_NEGATIVE

public static final int NO_NEGATIVE
Negative numbers are not allowed.

See Also:
Constant Field Values

NO_ZERO

public static final int NO_ZERO
Zero numbers are not allowed.

See Also:
Constant Field Values

NO_EMPTY

public static final int NO_EMPTY
Empty is not allowed. If not specified, empty usually means null.

See Also:
Constant Field Values

STRICT

public static final int STRICT
The value must match inside the data from ListModel only.

See Also:
Constant Field Values

SERVER

public static final int SERVER
Indicates this constraint requires the server validation. It means, after the client validates the value successfully, it will send the value to the server for further validation (by calling validate(org.zkoss.zk.ui.Component, java.lang.Object). It is useful if the result of the regular expressions is different at the client (with JavaScript) and the server with ava).

See Also:
Constant Field Values

NO_FUTURE

public static final int NO_FUTURE
Date in the future is not allowed. (Only date part is compared)

See Also:
Constant Field Values

NO_PAST

public static final int NO_PAST
Date in the past is not allowed. (Only date part is compared)

See Also:
Constant Field Values

NO_TODAY

public static final int NO_TODAY
Today is not allowed. (Only date part is compared)

See Also:
Constant Field Values

BEFORE_START

public static final int BEFORE_START
The Error-box position.

See Also:
Constant Field Values

BEFORE_END

public static final int BEFORE_END
The Error-box position.

See Also:
Constant Field Values

END_BEFORE

public static final int END_BEFORE
The Error-box position.

See Also:
Constant Field Values

END_AFTER

public static final int END_AFTER
The Error-box position.

See Also:
Constant Field Values

AFTER_END

public static final int AFTER_END
The Error-box position.

See Also:
Constant Field Values

AFTER_START

public static final int AFTER_START
The Error-box position.

See Also:
Constant Field Values

START_AFTER

public static final int START_AFTER
The Error-box position.

See Also:
Constant Field Values

START_BEFORE

public static final int START_BEFORE
The Error-box position.

See Also:
Constant Field Values

OVERLAP

public static final int OVERLAP
The Error-box position.

See Also:
Constant Field Values

OVERLAP_END

public static final int OVERLAP_END
The Error-box position.

See Also:
Constant Field Values

OVERLAP_BEFORE

public static final int OVERLAP_BEFORE
The Error-box position.

See Also:
Constant Field Values

OVERLAP_AFTER

public static final int OVERLAP_AFTER
The Error-box position.

See Also:
Constant Field Values

AT_POINTER

public static final int AT_POINTER
The Error-box position.

See Also:
Constant Field Values

AFTER_POINTER

public static final int AFTER_POINTER
The Error-box position.

See Also:
Constant Field Values

_flags

protected int _flags
The constraints. A combination of NO_POSITIVE and others.

Constructor Detail

SimpleConstraint

public SimpleConstraint(int flags)
Constructs a constraint with flags.

Parameters:
flags - a combination of NO_POSITIVE, NO_NEGATIVE, NO_ZERO, and so on.

SimpleConstraint

public SimpleConstraint(int flags,
                        java.lang.String errmsg)
Constructs a constraint with flags and an error message.

Parameters:
flags - a combination of NO_POSITIVE, NO_NEGATIVE, NO_ZERO, and so on.
errmsg - the error message to display. Ignored if null or empty.

SimpleConstraint

public SimpleConstraint(java.lang.String regex,
                        java.lang.String errmsg)
Constructs a regular-expression constraint.

Parameters:
regex - ignored if null or empty
errmsg - the error message to display. Ignored if null or empty.

SimpleConstraint

public SimpleConstraint(int flags,
                        java.lang.String regex,
                        java.lang.String errmsg)
Constructs a constraint combining regular expression.

Parameters:
flags - a combination of NO_POSITIVE, NO_NEGATIVE, NO_ZERO, and so on.
regex - ignored if null or empty
errmsg - the error message to display. Ignored if null or empty.

SimpleConstraint

public SimpleConstraint(java.lang.String constraint)
Constructs a constraint with a list of constraints separated by comma.

Parameters:
constraint - a list of constraints separated by comma. Example: no positive, no zero
Since:
3.0.2
Method Detail

getInstance

public static SimpleConstraint getInstance(java.lang.String constraint)
Parses a list of constraints from a string to an integer representing a combination of NO_POSITIVE and other flags.

Parameters:
constraint - a list of constraints separated by comma. Example: no positive, no zero

parseConstraint

protected int parseConstraint(java.lang.String constraint)
                       throws UiException
Parses a constraint into an integer value. For example, "no positive" is parsed to NO_POSITIVE.

Deriving classes might override this to provide more constraints.

Throws:
UiException
Since:
3.0.2

getFlags

public int getFlags()
Returns the constraint flags, i.e., a combination of NO_POSITIVE, NO_NEGATIVE, STRICT and others.

Since:
3.0.2

getErrorMessage

public java.lang.String getErrorMessage(Component comp)
Returns the custom error message that shall be shown if an error occurs, or null if no custom error message specified.


validate

public void validate(Component comp,
                     java.lang.Object value)
              throws WrongValueException
Description copied from interface: Constraint
Verifies whether the value is acceptable.

Specified by:
validate in interface Constraint
Parameters:
comp - the component being validated
Throws:
WrongValueException

getClientConstraint

public java.lang.String getClientConstraint()
Description copied from interface: ClientConstraint
Returns the JavaScript snippet that will be evaluated at client to return a validator, or null if no client constraint is supported. The validator is later used to validate an input.

For example,

String getClientConstraint() {
  return "new foo.MyValidator()";
}

Instead of return the snippet of JavaScript codes, it can return an instance of JavaScript string (enclosed with quotation), if the validator is zul.inp.SimpleConstraint.

For example,

String getClientConstraint() {
  return "'no empty'";
}

The validator could implement the validate, and showCustomError methods, and an optional property, serverValidate methods as follow. validate is required, while showCustomError and serverValidate are optional.

String validate(Widget wgt, String value);
Object showCustomError(Widget wgt, String errmsg);
boolean serverValidate;

Please refer to zul.inp.SimpleConstraint for details.

Notice that CustomConstraint has the higher priority than ClientConstraint. In other words, ClientConstraint is ignored if both defined.

Specified by:
getClientConstraint in interface ClientConstraint
Returns:
the code snippet that will be evaluated at client to return a validator.

getClientPackages

public java.lang.String getClientPackages()
Default: null (since it depends on zul.inp which is loaded for all input widgets).

Specified by:
getClientPackages in interface ClientConstraint


Copyright © 2005-2011 Potix Corporation. All Rights Reserved. SourceForge.net Logo