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):
Modifier and Type | Class and Description |
---|---|
static class |
Validator.HighlightMode
Indicates the validation failure modes
|
Constructor and Description |
---|
Validator()
Default constructor
|
Modifier and Type | Method and Description |
---|---|
Validator |
addConstraint(Component cmp,
Constraint... c)
Places a constraint on the validator, returns this object so constraint
additions can be chained.
|
Validator |
addSubmitButtons(Component... cmp)
Submit buttons (or any other component type) can be disabled until all components contain a valid value.
|
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!
|
protected Object |
getComponentValue(Component cmp)
Returns the value of the given component, this can be overriden to add support for custom built components
|
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.
|
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.
|
static Image |
getDefaultValidationFailedEmblem()
The emblem that will be drawn on top of the component to indicate the validation failure
|
static Validator.HighlightMode |
getDefaultValidationFailureHighlightMode()
Indicates the default mode in which validation failures are expressed
|
String |
getErrorMessage(Component cmp)
Returns the validation error message for the given component or null if no such message exists
|
String |
getErrorMessageUIID()
Error message UIID defaults to DialogBody.
|
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.
|
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.
|
Image |
getValidationFailedEmblem()
The emblem that will be drawn on top of the component to indicate the validation failure
|
Validator.HighlightMode |
getValidationFailureHighlightMode()
Indicates the default mode in which validation failures are expressed
|
boolean |
isShowErrorMessageForFocusedComponent()
Indicates whether an error message should be shown for the focused component
|
boolean |
isValid()
Returns true if all the constraints are currently valid
|
static boolean |
isValidateOnEveryKey()
Indicates whether validation should occur on every key press (data change listener) or
action performed (editing completion)
|
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.
|
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.
|
static void |
setDefaultValidationFailedEmblem(Image aDefaultValidationFailedEmblem)
The emblem that will be drawn on top of the component to indicate the validation failure
|
static void |
setDefaultValidationFailureHighlightMode(Validator.HighlightMode aDefaultValidationFailureHighlightMode)
Indicates the default mode in which validation failures are expressed
|
void |
setErrorMessageUIID(String errorMessageUIID)
Error message UIID defaults to DialogBody.
|
void |
setShowErrorMessageForFocusedComponent(boolean showErrorMessageForFocusedComponent)
Indicates whether an error message should be shown for the focused component
|
static void |
setValidateOnEveryKey(boolean aValidateOnEveryKey)
Indicates whether validation should occur on every key press (data change listener) or
action performed (editing completion)
|
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.
|
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.
|
void |
setValidationFailedEmblem(Image validationFailedEmblem)
The emblem that will be drawn on top of the component to indicate the validation failure
|
void |
setValidationFailureHighlightMode(Validator.HighlightMode validationFailureHighlightMode)
Indicates the default mode in which validation failures are expressed
|
protected void |
validate(Component cmp)
Validates and highlights an individual component
|
public static Validator.HighlightMode getDefaultValidationFailureHighlightMode()
public static void setDefaultValidationFailureHighlightMode(Validator.HighlightMode aDefaultValidationFailureHighlightMode)
aDefaultValidationFailureHighlightMode
- the defaultValidationFailureHighlightMode to setpublic static Image getDefaultValidationFailedEmblem()
public static void setDefaultValidationFailedEmblem(Image aDefaultValidationFailedEmblem)
aDefaultValidationFailedEmblem
- the defaultValidationFailedEmblem to setpublic static float getDefaultValidationEmblemPositionX()
public static void setDefaultValidationEmblemPositionX(float aDefaultValidationEmblemPositionX)
aDefaultValidationEmblemPositionX
- the defaultValidationEmblemPositionX to setpublic static float getDefaultValidationEmblemPositionY()
public static void setDefaultValidationEmblemPositionY(float aDefaultValidationEmblemPositionY)
aDefaultValidationEmblemPositionY
- the defaultValidationEmblemPositionY to setpublic static boolean isValidateOnEveryKey()
public static void setValidateOnEveryKey(boolean aValidateOnEveryKey)
aValidateOnEveryKey
- the validateOnEveryKey to setpublic Validator.HighlightMode getValidationFailureHighlightMode()
public void setValidationFailureHighlightMode(Validator.HighlightMode validationFailureHighlightMode)
validationFailureHighlightMode
- the validationFailureHighlightMode to setpublic Image getValidationFailedEmblem()
public void setValidationFailedEmblem(Image validationFailedEmblem)
validationFailedEmblem
- the validationFailedEmblem to setpublic float getValidationEmblemPositionX()
public void setValidationEmblemPositionX(float validationEmblemPositionX)
validationEmblemPositionX
- the validationEmblemPositionX to setpublic float getValidationEmblemPositionY()
public void setValidationEmblemPositionY(float validationEmblemPositionY)
validationEmblemPositionY
- the validationEmblemPositionY to setpublic boolean isShowErrorMessageForFocusedComponent()
public void setShowErrorMessageForFocusedComponent(boolean showErrorMessageForFocusedComponent)
showErrorMessageForFocusedComponent
- true to show the error messagepublic String getErrorMessageUIID()
public void setErrorMessageUIID(String errorMessageUIID)
errorMessageUIID
- the errorMessageUIID to setpublic Validator addConstraint(Component cmp, Constraint... c)
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.cmp
- the component to validatec
- the constraint or constraintspublic Validator addSubmitButtons(Component... cmp)
cmp
- set of buttons or components to disable until everything is validprotected Object getComponentValue(Component cmp)
cmp
- the componentpublic void bindDataListener(Component cmp)
cmp
- the component to bind the data listener topublic boolean isValid()
protected void validate(Component cmp)
cmp
- the component to validate