Class SimpleConstraint
- java.lang.Object
-
- org.zkoss.zul.SimpleConstraint
-
- All Implemented Interfaces:
java.io.Serializable
,ClientConstraint
,Constraint
- Direct Known Subclasses:
AbstractSimpleDateTimeConstraint
,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
andDatebox
, you could combine the flags, such asNO_POSITIVE
+NO_ZERO
to accept only negative number.- Author:
- tomyeh
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected boolean
_finishParseCst
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
Constructors Constructor Description 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.util.regex.Pattern 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.util.regex.Pattern regex, java.lang.String errmsg)
Constructs a regular-expression constraint.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description 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()
static SimpleConstraint
getInstance(java.lang.String constraint)
Parses a list of constraints from a string to an integer representing a combination ofNO_POSITIVE
and other flags.java.lang.String
getRawValue()
Returns the raw valueprotected 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.
-
-
-
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 callingvalidate(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 ofNO_POSITIVE
and others.
-
_finishParseCst
protected boolean _finishParseCst
-
-
Constructor Detail
-
SimpleConstraint
public SimpleConstraint(int flags)
Constructs a constraint with flags.- Parameters:
flags
- a combination ofNO_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 ofNO_POSITIVE
,NO_NEGATIVE
,NO_ZERO
, and so on.errmsg
- the error message to display. Ignored if null or empty.
-
SimpleConstraint
public SimpleConstraint(java.util.regex.Pattern regex, java.lang.String errmsg)
Constructs a regular-expression constraint.- Parameters:
regex
- ignored if null or emptyerrmsg
- the error message to display. Ignored if null or empty.- Since:
- 8.0.1
-
SimpleConstraint
public SimpleConstraint(int flags, java.util.regex.Pattern regex, java.lang.String errmsg)
Constructs a constraint combining regular expression.- Parameters:
flags
- a combination ofNO_POSITIVE
,NO_NEGATIVE
,NO_ZERO
, and so on.regex
- ignored if null or emptyerrmsg
- the error message to display. Ignored if null or empty.- Since:
- 8.0.1
-
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
, or use '/' to enclose the regular expression as follows./.+@.+\.[a-z]+/: email only
- 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 ofNO_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 toNO_POSITIVE
.Deriving classes might override this to provide more constraints.
- Throws:
UiException
- Since:
- 3.0.2
-
getFlags
public int getFlags()
- 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 interfaceConstraint
- 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
, andshowCustomError
methods, and an optional property,serverValidate
methods as follow.validate
is required, whileshowCustomError
andserverValidate
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 thanClientConstraint
. In other words,ClientConstraint
is ignored if both defined.- Specified by:
getClientConstraint
in interfaceClientConstraint
- 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 interfaceClientConstraint
-
getRawValue
public java.lang.String getRawValue()
Returns the raw value- Since:
- 10.0.0
-
-