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, SimpleSpinnerConstraint

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

A simple constraint that you could build based the predefined constants.

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 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
          Postive numbers are not allowed.
static int NO_TODAY
          Today is not allowed.
static int NO_ZERO
          Zero numbers are not allowed.
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 getClientValidation()
          Returns the function name in JavaScript or a Javascript code snippet used to validate the value at the client, or null if no client verification is supported.
 java.lang.String getErrorMessage(Component comp)
          Returns the error message when the client detects an error, or null if not 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.
 boolean isClientComplete()
          Returns whether the client's validation is complete.
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
Postive 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

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

_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 final 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

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

getClientValidation

public java.lang.String getClientValidation()
Description copied from interface: ClientConstraint
Returns the function name in JavaScript or a Javascript code snippet used to validate the value at the client, or null if no client verification is supported.

There are two formats of the return value:

Format 1:
Syntax: function_name
Example: "zkVld.noEmpty"
What Really Happens:
zkVld.noEmpty('id') is called at the client side to validate the input, where id is the component's identifier.

Format 2:
Syntax: function_name(arg1, arg2, arg3)
where arg could be #{EL_expression}
Example: "myValid(#{self},#{when},'more')"
What Really Happens:
myValid($e('id'),new Date('2007/06/03'),'more') is called at the client side to validate the input, where id is the component's identifier.

Specified by:
getClientValidation in interface ClientConstraint

getErrorMessage

public java.lang.String getErrorMessage(Component comp)
Description copied from interface: ClientConstraint
Returns the error message when the client detects an error, or null if not specified.

It is used only if you want to override the default error message shown by the client. It won't affect the message caused by an exception thrown by Constraint.validate(org.zkoss.zk.ui.Component, java.lang.Object).

Specified by:
getErrorMessage in interface ClientConstraint

isClientComplete

public boolean isClientComplete()
Description copied from interface: ClientConstraint
Returns whether the client's validation is complete. If true, onChange won't be sent immediately (unless onChange is listened). If false, onChange is always sent no matter ClientConstraint.getClientValidation() return null or not.

Specified by:
isClientComplete in interface ClientConstraint


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