public class Validator
- Object
- 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.HighlightMode | Indicates 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
aDefaultValidationFailureHighlightModeValidator.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
aDefaultValidationFailedEmblemImage- 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
aDefaultValidationEmblemPositionXfloat- 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
aDefaultValidationEmblemPositionYfloat- 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
aValidateOnEveryKeyboolean- 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
validationFailureHighlightModeValidator.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
validationFailedEmblemImage- 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
validationEmblemPositionXfloat- 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
validationEmblemPositionYfloat- 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
showErrorMessageForFocusedComponentboolean- 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
errorMessageUIIDString- 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
cmpComponent- the component to validate
cConstraint...- 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
cmpComponent...- 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
cmpComponent- 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
cmpComponent- 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
cmpComponent- 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
cmpComponent- the invalid component
Returns
a string representing the error message