Class Validator

java.lang.Object
com.codename1.ui.validation.Validator

public class Validator extends Object

Binds validation constraints to form elements, when validation fails it can be highlighted directly on the component via an emblem or change of the UIID (to original UIID name + "Invalid" e.g. "TextFieldInvalid"). Validators just run thru a set of Constraint objects to decide if validation succeeded or failed.

It's possible to create any custom logic of validation. Example (see this discussion on StackOverflow):

val.addConstraint(phone, new Constraint() {
  public  boolean isValid(Object value) {
    String v = (String)value;
    for(int i = 0 ; i = '0' && c <= '9' || c == '+' || c == '-') {
        continue;
      }
      return false;
    }
    return true;
  }
  public String getDefaultFailMessage() {
    return "Must be valid phone number";
  }
});
  • Constructor Details

    • Validator

      public Validator()
      Default constructor
  • Method Details

    • getDefaultValidationFailureHighlightMode

      public static Validator.HighlightMode getDefaultValidationFailureHighlightMode()

      Indicates the default mode in which validation failures are expressed

      Returns

      the defaultValidationFailureHighlightMode

    • setDefaultValidationFailureHighlightMode

      public static void setDefaultValidationFailureHighlightMode(Validator.HighlightMode aDefaultValidationFailureHighlightMode)

      Indicates the default mode in which validation failures are expressed

      Parameters
      • aDefaultValidationFailureHighlightMode: the defaultValidationFailureHighlightMode to set
    • getDefaultValidationFailedEmblem

      public static Image getDefaultValidationFailedEmblem()

      The emblem that will be drawn on top of the component to indicate the validation failure

      Returns

      the defaultValidationFailedEmblem

    • setDefaultValidationFailedEmblem

      public static void setDefaultValidationFailedEmblem(Image aDefaultValidationFailedEmblem)

      The emblem that will be drawn on top of the component to indicate the validation failure

      Parameters
      • aDefaultValidationFailedEmblem: the defaultValidationFailedEmblem to set
    • getDefaultValidationEmblemPositionX

      public static float getDefaultValidationEmblemPositionX()

      The position of the validation emblem on the component as X/Y values between 0 and 1 where 0 indicates the start of the component and 1 indicates its end on the given axis.

      Returns

      the defaultValidationEmblemPositionX

    • setDefaultValidationEmblemPositionX

      public static void setDefaultValidationEmblemPositionX(float aDefaultValidationEmblemPositionX)

      The position of the validation emblem on the component as X/Y values between 0 and 1 where 0 indicates the start of the component and 1 indicates its end on the given axis.

      Parameters
      • aDefaultValidationEmblemPositionX: the defaultValidationEmblemPositionX to set
    • getDefaultValidationEmblemPositionY

      public static float getDefaultValidationEmblemPositionY()

      The position of the validation emblem on the component as X/Y values between 0 and 1 where 0 indicates the start of the component and 1 indicates its end on the given axis.

      Returns

      the defaultValidationEmblemPositionY

    • setDefaultValidationEmblemPositionY

      public static void setDefaultValidationEmblemPositionY(float aDefaultValidationEmblemPositionY)

      The position of the validation emblem on the component as X/Y values between 0 and 1 where 0 indicates the start of the component and 1 indicates its end on the given axis.

      Parameters
      • aDefaultValidationEmblemPositionY: the defaultValidationEmblemPositionY to set
    • isValidateOnEveryKey

      public static boolean isValidateOnEveryKey()

      Indicates whether validation should occur on every key press (data change listener) or action performed (editing completion)

      Returns

      the validateOnEveryKey

    • setValidateOnEveryKey

      public static void setValidateOnEveryKey(boolean aValidateOnEveryKey)

      Indicates whether validation should occur on every key press (data change listener) or action performed (editing completion)

      Parameters
      • aValidateOnEveryKey: the validateOnEveryKey to set
    • getValidationFailureHighlightMode

      public Validator.HighlightMode getValidationFailureHighlightMode()

      Indicates the default mode in which validation failures are expressed

      Returns

      the validationFailureHighlightMode

    • setValidationFailureHighlightMode

      public void setValidationFailureHighlightMode(Validator.HighlightMode validationFailureHighlightMode)

      Indicates the default mode in which validation failures are expressed

      Parameters
      • validationFailureHighlightMode: the validationFailureHighlightMode to set
    • getValidationFailedEmblem

      public Image getValidationFailedEmblem()

      The emblem that will be drawn on top of the component to indicate the validation failure

      Returns

      the validationFailedEmblem

    • setValidationFailedEmblem

      public void setValidationFailedEmblem(Image validationFailedEmblem)

      The emblem that will be drawn on top of the component to indicate the validation failure

      Parameters
      • validationFailedEmblem: the validationFailedEmblem to set
    • getValidationEmblemPositionX

      public float getValidationEmblemPositionX()

      The position of the validation emblem on the component as X/Y values between 0 and 1 where 0 indicates the start of the component and 1 indicates its end on the given axis.

      Returns

      the validationEmblemPositionX

    • setValidationEmblemPositionX

      public void setValidationEmblemPositionX(float validationEmblemPositionX)

      The position of the validation emblem on the component as X/Y values between 0 and 1 where 0 indicates the start of the component and 1 indicates its end on the given axis.

      Parameters
      • validationEmblemPositionX: the validationEmblemPositionX to set
    • getValidationEmblemPositionY

      public float getValidationEmblemPositionY()

      The position of the validation emblem on the component as X/Y values between 0 and 1 where 0 indicates the start of the component and 1 indicates its end on the given axis.

      Returns

      the validationEmblemPositionY

    • setValidationEmblemPositionY

      public void setValidationEmblemPositionY(float validationEmblemPositionY)

      The position of the validation emblem on the component as X/Y values between 0 and 1 where 0 indicates the start of the component and 1 indicates its end on the given axis.

      Parameters
      • validationEmblemPositionY: the validationEmblemPositionY to set
    • isShowErrorMessageForFocusedComponent

      public boolean isShowErrorMessageForFocusedComponent()

      Indicates whether an error message should be shown for the focused component

      Returns

      true if the error message should be displayed

    • setShowErrorMessageForFocusedComponent

      public void setShowErrorMessageForFocusedComponent(boolean showErrorMessageForFocusedComponent)

      Indicates whether an error message should be shown for the focused component

      Parameters
      • showErrorMessageForFocusedComponent: true to show the error message
    • getErrorMessageUIID

      public String getErrorMessageUIID()

      Error message UIID defaults to DialogBody. Allows customizing the look of the message

      Returns

      the errorMessageUIID

    • setErrorMessageUIID

      public void setErrorMessageUIID(String errorMessageUIID)

      Error message UIID defaults to DialogBody. Allows customizing the look of the message

      Parameters
      • errorMessageUIID: the errorMessageUIID to set
    • addConstraint

      public Validator addConstraint(Component cmp, Constraint... c)

      Places a constraint on the validator, returns this object so constraint additions can be chained. Shows validation errors messages even when the TextModeLayout is not onTopMode (it's possible to disable this functionality setting to false the theme constant showValidationErrorsIfNotOnTopMode: basically, the error message is shown for two second in place of the label on the left of the InputComponent (or on right of the InputComponent for RTL languages); this solution never breaks the layout, because the error message is trimmed to fit the available space. The error message UIID is "ErrorLabel" when it's not onTopMode.

      Parameters
      • cmp: the component to validate

      • c: the constraint or constraints

      Returns
      Returns:
      this object so we can write code like v.addConstraint(cmp1, cons).addConstraint(cmp2, otherConstraint);
    • addSubmitButtons

      public Validator addSubmitButtons(Component... cmp)

      Submit buttons (or any other component type) can be disabled until all components contain a valid value. Notice that this method should be invoked after all the constraints are added so the initial state of the buttons will be correct.

      Parameters
      • cmp: set of buttons or components to disable until everything is valid
      Returns

      the validator instance so this method can be chained

    • getComponentValue

      protected Object getComponentValue(Component cmp)

      Returns the value of the given component, this can be overriden to add support for custom built components

      Parameters
      • cmp: the component
      Returns

      the object value

    • bindDataListener

      public void bindDataListener(Component cmp)

      Binds an event listener to the given component

      Parameters
      • cmp: the component to bind the data listener to
      Deprecated
    • isValid

      public boolean isValid()

      Returns true if all the constraints are currently valid

      Returns

      true if the entire validator is valid

    • validate

      protected void validate(Component cmp)

      Validates and highlights an individual component

      Parameters
      • cmp: the component to validate
    • getErrorMessage

      public String getErrorMessage(Component cmp)

      Returns the validation error message for the given component or null if no such message exists

      Parameters
      • cmp: the invalid component
      Returns

      a string representing the error message