public class Validator

  1. Object
  2. Validator

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):

Form hi = new Form("Phone Validator", BoxLayout.y());
TextField phone = new TextField("", "Phone");
Validator val = new Validator();
val.addConstraint(phone, new Constraint() {
  public  boolean isValid(Object value) {
    String v = (String)value;
    for (int i = 0; i < v.length(); i++) {
      char c = v.charAt(i);
      if ((c >= '0' && c <= '9') || c == '+' || c == '-') {
        continue;
      }
      return false;
    }
    return true;
  }
  public String getDefaultFailMessage() {
    return "Must be valid phone number";
  }
});
hi.add(phone);
hi.show();

Nested types

enum Validator.HighlightModeIndicates the validation failure modes

Constructors

public Validator()Default constructor

Methods

public static Validator.HighlightMode getDefaultValidationFailureHighlightMode()Indicates the default mode in which validation failures are expressed
public static void setDefaultValidationFailureHighlightMode(Validator.HighlightMode aDefaultValidationFailureHighlightMode)Indicates the default mode in which validation failures are expressed
public static Image getDefaultValidationFailedEmblem()The emblem that will be drawn on top of the component to indicate the validation failure
public static void setDefaultValidationFailedEmblem(Image aDefaultValidationFailedEmblem)The emblem that will be drawn on top of the component to indicate the validation failure
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.
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.
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.
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.
public static boolean isValidateOnEveryKey()Indicates whether validation should occur on every key press (data change listener) or action performed (editing completion)
public static void setValidateOnEveryKey(boolean aValidateOnEveryKey)Indicates whether validation should occur on every key press (data change listener) or action performed (editing completion)
public Validator.HighlightMode getValidationFailureHighlightMode()Indicates the default mode in which validation failures are expressed
public void setValidationFailureHighlightMode(Validator.HighlightMode validationFailureHighlightMode)Indicates the default mode in which validation failures are expressed
public Image getValidationFailedEmblem()The emblem that will be drawn on top of the component to indicate the validation failure
public void setValidationFailedEmblem(Image validationFailedEmblem)The emblem that will be drawn on top of the component to indicate the validation failure
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.
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.
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.
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.
public boolean isShowErrorMessageForFocusedComponent()Indicates whether an error message should be shown for the focused component
public void setShowErrorMessageForFocusedComponent(boolean showErrorMessageForFocusedComponent)Indicates whether an error message should be shown for the focused component
public String getErrorMessageUIID()Error message UIID defaults to DialogBody.
public void setErrorMessageUIID(String errorMessageUIID)Error message UIID defaults to DialogBody.
public Validator addConstraint(Component cmp, Constraint... c)Places a constraint on the validator, returns this object so constraint additions can be chained.
public Validator addSubmitButtons(Component... cmp)Submit buttons (or any other component type) can be disabled until all components contain a valid value.
protected Object getComponentValue(Component cmp)Returns the value of the given component, this can be overriden to add support for custom built components
public void bindDataListener(Component cmp)Deprecated Binds an event listener to the given component
public boolean isValid()Returns true if all the constraints are currently valid
protected void validate(Component cmp)Validates and highlights an individual component
public String getErrorMessage(Component cmp)Returns the validation error message for the given component or null if no such message exists

Inherited methods

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 Validator.HighlightMode
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 Image
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 float
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 float
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 boolean
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 Validator.HighlightMode
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 Image
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 float
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 float
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 boolean
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 String
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 Component
the component to validate
c Constraint...
the constraint or constraints

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 Component...
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 Component
the component

Returns

the object value

bindDataListener

public void bindDataListener(Component cmp)
Deprecated. this method was exposed by accident, constraint implicitly calls it and you don’t need to call it directly. It will be made protected in a future update to Codename One!
Binds an event listener to the given component

Parameters

cmp Component
the component to bind the data listener to

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 Component
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 Component
the invalid component

Returns

a string representing the error message