Class 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):
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";
}
});
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumIndicates the validation failure modes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddConstraint(Component cmp, Constraint... c) Places a constraint on the validator, returns this object so constraint additions can be chained.addSubmitButtons(Component... cmp) Submit buttons (or any other component type) can be disabled until all components contain a valid value.voidBinds an event listener to the given componentprotected ObjectReturns the value of the given component, this can be overriden to add support for custom built componentsstatic floatThe 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.static floatThe 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.static ImageThe emblem that will be drawn on top of the component to indicate the validation failurestatic Validator.HighlightModeIndicates the default mode in which validation failures are expressedgetErrorMessage(Component cmp) Returns the validation error message for the given component or null if no such message existsError message UIID defaults to DialogBody.floatThe 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.floatThe 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.The emblem that will be drawn on top of the component to indicate the validation failureIndicates the default mode in which validation failures are expressedbooleanIndicates whether an error message should be shown for the focused componentbooleanisValid()Returns true if all the constraints are currently validstatic booleanIndicates whether validation should occur on every key press (data change listener) or action performed (editing completion)static voidsetDefaultValidationEmblemPositionX(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.static voidsetDefaultValidationEmblemPositionY(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.static voidsetDefaultValidationFailedEmblem(Image aDefaultValidationFailedEmblem) The emblem that will be drawn on top of the component to indicate the validation failurestatic voidsetDefaultValidationFailureHighlightMode(Validator.HighlightMode aDefaultValidationFailureHighlightMode) Indicates the default mode in which validation failures are expressedvoidsetErrorMessageUIID(String errorMessageUIID) Error message UIID defaults to DialogBody.voidsetShowErrorMessageForFocusedComponent(boolean showErrorMessageForFocusedComponent) Indicates whether an error message should be shown for the focused componentstatic voidsetValidateOnEveryKey(boolean aValidateOnEveryKey) Indicates whether validation should occur on every key press (data change listener) or action performed (editing completion)voidsetValidationEmblemPositionX(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.voidsetValidationEmblemPositionY(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.voidsetValidationFailedEmblem(Image validationFailedEmblem) The emblem that will be drawn on top of the component to indicate the validation failurevoidsetValidationFailureHighlightMode(Validator.HighlightMode validationFailureHighlightMode) Indicates the default mode in which validation failures are expressedprotected voidValidates and highlights an individual component
-
Constructor Details
-
Validator
public Validator()Default constructor
-
-
Method Details
-
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
The emblem that will be drawn on top of the component to indicate the validation failure
Returns
the defaultValidationFailedEmblem
-
setDefaultValidationFailedEmblem
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
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
The emblem that will be drawn on top of the component to indicate the validation failure
Returns
the validationFailedEmblem
-
setValidationFailedEmblem
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
Error message UIID defaults to DialogBody. Allows customizing the look of the message
Returns
the errorMessageUIID
-
setErrorMessageUIID
Error message UIID defaults to DialogBody. Allows customizing the look of the message
Parameters
errorMessageUIID: the errorMessageUIID to set
-
addConstraint
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 constantshowValidationErrorsIfNotOnTopMode: 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
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
-
bindDataListener
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
Validates and highlights an individual component
Parameters
cmp: the component to validate
-
getErrorMessage
-