Package org.zkoss.zul

Class SimpleConstraint

java.lang.Object
org.zkoss.zul.SimpleConstraint
All Implemented Interfaces:
Serializable, ClientConstraint, Constraint
Direct Known Subclasses:
AbstractSimpleDateTimeConstraint, SimpleNumberInputConstraint

public class SimpleConstraint extends Object implements Constraint, ClientConstraint, 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:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected boolean
     
    protected int
    The constraints.
    static final int
    The Error-box position.
    static final int
    The Error-box position.
    static final int
    The Error-box position.
    static final int
    The Error-box position.
    static final int
    The Error-box position.
    static final int
    The Error-box position.
    static final int
    The Error-box position.
    static final int
    The Error-box position.
    static final int
    Empty is not allowed.
    static final int
    Date in the future is not allowed.
    static final int
    Negative numbers are not allowed.
    static final int
    Date in the past is not allowed.
    static final int
    Positive numbers are not allowed.
    static final int
    Today is not allowed.
    static final int
    Zero numbers are not allowed.
    static final int
    The Error-box position.
    static final int
    The Error-box position.
    static final int
    The Error-box position.
    static final int
    The Error-box position.
    static final int
    Indicates that this constraint requires immediate server validation.
    static final int
    The Error-box position.
    static final int
    The Error-box position.
    static final int
    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, String errmsg)
    Constructs a constraint with flags and an error message.
    SimpleConstraint(int flags, Pattern regex, String errmsg)
    Constructs a constraint combining regular expression.
    Constructs a constraint with a list of constraints separated by comma.
    Constructs a regular-expression constraint.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the JavaScript snippet that will be evaluated at client to return a validator, or null if no client constraint is supported.
    Default: null (since it depends on zul.inp which is loaded for all input widgets).
    getErrorMessage(org.zkoss.zk.ui.Component comp)
    Returns the custom error message that shall be shown if an error occurs, or null if no custom error message specified.
    int
    Returns the constraint flags, i.e., a combination of NO_POSITIVE, NO_NEGATIVE, STRICT and others.
    getInstance(String constraint)
    Parses a list of constraints from a string to an integer representing a combination of NO_POSITIVE and other flags.
    Returns the raw value
    protected int
    parseConstraint(String constraint)
    Parses a constraint into an integer value.
    void
    validate(org.zkoss.zk.ui.Component comp, 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 Details

    • NO_POSITIVE

      public static final int NO_POSITIVE
      Positive numbers are not allowed.
      See Also:
    • NO_NEGATIVE

      public static final int NO_NEGATIVE
      Negative numbers are not allowed.
      See Also:
    • NO_ZERO

      public static final int NO_ZERO
      Zero numbers are not allowed.
      See Also:
    • NO_EMPTY

      public static final int NO_EMPTY
      Empty is not allowed. If not specified, empty usually means null.
      See Also:
    • STRICT

      public static final int STRICT
      The value must match inside the data from ListModel only.
      See Also:
    • SERVER

      public static final int SERVER
      Indicates that this constraint requires immediate server validation.

      If this flag is set, after client-side validation succeeds, the value will be immediately sent to the server for validation by calling validate(org.zkoss.zk.ui.Component, java.lang.Object).

      If this flag is not set, the validated value will be queued similarly to regular unmonitored events and sent to the server at a later time.

      See Also:
    • NO_FUTURE

      public static final int NO_FUTURE
      Date in the future is not allowed. (Only date part is compared)
      See Also:
    • NO_PAST

      public static final int NO_PAST
      Date in the past is not allowed. (Only date part is compared)
      See Also:
    • NO_TODAY

      public static final int NO_TODAY
      Today is not allowed. (Only date part is compared)
      See Also:
    • BEFORE_START

      public static final int BEFORE_START
      The Error-box position.
      See Also:
    • BEFORE_END

      public static final int BEFORE_END
      The Error-box position.
      See Also:
    • END_BEFORE

      public static final int END_BEFORE
      The Error-box position.
      See Also:
    • END_AFTER

      public static final int END_AFTER
      The Error-box position.
      See Also:
    • AFTER_END

      public static final int AFTER_END
      The Error-box position.
      See Also:
    • AFTER_START

      public static final int AFTER_START
      The Error-box position.
      See Also:
    • START_AFTER

      public static final int START_AFTER
      The Error-box position.
      See Also:
    • START_BEFORE

      public static final int START_BEFORE
      The Error-box position.
      See Also:
    • OVERLAP

      public static final int OVERLAP
      The Error-box position.
      See Also:
    • OVERLAP_END

      public static final int OVERLAP_END
      The Error-box position.
      See Also:
    • OVERLAP_BEFORE

      public static final int OVERLAP_BEFORE
      The Error-box position.
      See Also:
    • OVERLAP_AFTER

      public static final int OVERLAP_AFTER
      The Error-box position.
      See Also:
    • AT_POINTER

      public static final int AT_POINTER
      The Error-box position.
      See Also:
    • AFTER_POINTER

      public static final int AFTER_POINTER
      The Error-box position.
      See Also:
    • _flags

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

      protected boolean _finishParseCst
  • Constructor Details

    • 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, 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(Pattern regex, 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.
      Since:
      8.0.1
    • SimpleConstraint

      public SimpleConstraint(int flags, Pattern regex, 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.
      Since:
      8.0.1
    • SimpleConstraint

      public SimpleConstraint(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 Details

    • getInstance

      public static SimpleConstraint getInstance(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(String constraint) throws org.zkoss.zk.ui.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:
      org.zkoss.zk.ui.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 String getErrorMessage(org.zkoss.zk.ui.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(org.zkoss.zk.ui.Component comp, Object value) throws org.zkoss.zk.ui.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:
      org.zkoss.zk.ui.WrongValueException
    • getClientConstraint

      public 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 String getClientPackages()
      Default: null (since it depends on zul.inp which is loaded for all input widgets).
      Specified by:
      getClientPackages in interface ClientConstraint
    • getRawValue

      public String getRawValue()
      Returns the raw value
      Since:
      10.0.0