public class ValidationSupport
extends java.lang.Object
Validators can be registered for components, to provide the validation:
ValidationSupport validationSupport = new ValidationSupport();
validationSupport.registerValidator(textField, Validator.createEmptyValidator("Text is required"));
validationSupport.registerValidator(combobox, Validator.createEmptyValidator( "ComboBox Selection required"));
validationSupport.registerValidator(checkBox, (Control c, Boolean newValue) ->
ValidationResult.fromErrorIf( c, "Checkbox should be checked", !newValue)
);
validationResultProperty provides an ability to react on overall validation result changes:
validationSupport.validationResultProperty().addListener( (o, oldValue, newValue) ->
messageList.getItems().setAll(newValue.getMessages()));
Standard JavaFX UI controls are supported out of the box. There is also an ability to add support for custom controls.
To do that "observable value extractor" should be added for specific controls. Such "extractor" consists of two functional interfaces:
a Predicate to check the applicability of the control and a Callback to extract control's observable value.
Here is an sample of internal registration of such "extractor" for a few controls :
ValueExtractor.addObservableValueExtractor( c -> c instanceof TextInputControl, c -> ((TextInputControl)c).textProperty());
ValueExtractor.addObservableValueExtractor( c -> c instanceof ComboBox, c -> ((ComboBox<?>)c).getValue());
| Type | Property and Description |
|---|---|
javafx.beans.property.BooleanProperty |
errorDecorationEnabled |
javafx.beans.property.ReadOnlyBooleanProperty |
invalid
Validation state property
|
javafx.beans.property.ObjectProperty<ValidationDecoration> |
validationDecorator |
javafx.beans.property.ReadOnlyObjectProperty<ValidationResult> |
validationResult
Can be used to track validation result changes
|
| Constructor and Description |
|---|
ValidationSupport()
Creates validation support instance.
|
| Modifier and Type | Method and Description |
|---|---|
javafx.beans.property.BooleanProperty |
errorDecorationEnabledProperty() |
java.util.Optional<ValidationMessage> |
getHighestMessage(javafx.scene.control.Control target)
Returns optional highest severity message for a control
|
java.util.Set<javafx.scene.control.Control> |
getRegisteredControls()
Returns currently registered controls
|
ValidationDecoration |
getValidationDecorator()
Returns current validation decorator
|
ValidationResult |
getValidationResult()
Retrieves current validation result
|
void |
initInitialDecoration()
Activates the initial decoration of validated controls.
|
javafx.beans.property.ReadOnlyBooleanProperty |
invalidProperty()
Validation state property
|
java.lang.Boolean |
isInvalid()
Returns current validation state.
|
static boolean |
isRequired(javafx.scene.control.Control c)
Check control's required flag
|
void |
redecorate()
Redecorates all known components
Only decorations related to validation are affected
|
<T> boolean |
registerValidator(javafx.scene.control.Control c,
boolean required,
Validator<T> validator)
Registers
Validator for specified control with additional possiblity to mark control as required or not. |
<T> boolean |
registerValidator(javafx.scene.control.Control c,
Validator<T> validator)
Registers
Validator for specified control and makes control required |
void |
revalidate()
Triggers validation for all known components.
|
void |
revalidate(javafx.scene.control.Control c)
Triggers validation for the given component.
|
void |
setErrorDecorationEnabled(boolean enabled)
Sets the value of the property errorDecorationEnabled.
|
static void |
setRequired(javafx.scene.control.Control c,
boolean required)
Set control's required flag
|
void |
setValidationDecorator(ValidationDecoration decorator)
Sets new validation decorator
|
javafx.beans.property.ObjectProperty<ValidationDecoration> |
validationDecoratorProperty() |
javafx.beans.property.ReadOnlyObjectProperty<ValidationResult> |
validationResultProperty()
Can be used to track validation result changes
|
public javafx.beans.property.BooleanProperty errorDecorationEnabledProperty
setErrorDecorationEnabled(boolean)public javafx.beans.property.ReadOnlyObjectProperty<ValidationResult> validationResultProperty
getValidationResult()public javafx.beans.property.ReadOnlyBooleanProperty invalidProperty
isInvalid()public javafx.beans.property.ObjectProperty<ValidationDecoration> validationDecoratorProperty
public ValidationSupport()
initInitialDecoration().public static void setRequired(javafx.scene.control.Control c,
boolean required)
c - controlrequired - flagpublic static boolean isRequired(javafx.scene.control.Control c)
c - controlpublic void initInitialDecoration()
public void redecorate()
public void revalidate()
public void revalidate(javafx.scene.control.Control c)
public javafx.beans.property.BooleanProperty errorDecorationEnabledProperty()
setErrorDecorationEnabled(boolean)public void setErrorDecorationEnabled(boolean enabled)
public ValidationResult getValidationResult()
public javafx.beans.property.ReadOnlyObjectProperty<ValidationResult> validationResultProperty()
getValidationResult()public java.lang.Boolean isInvalid()
public javafx.beans.property.ReadOnlyBooleanProperty invalidProperty()
isInvalid()public javafx.beans.property.ObjectProperty<ValidationDecoration> validationDecoratorProperty()
public ValidationDecoration getValidationDecorator()
public void setValidationDecorator(ValidationDecoration decorator)
decorator - new validation decorator. Null value is valid - no decoration will occurpublic <T> boolean registerValidator(javafx.scene.control.Control c,
boolean required,
Validator<T> validator)
Validator for specified control with additional possiblity to mark control as required or not.c - control to validaterequired - true if controls should be requiredvalidator - Validator to be usedpublic <T> boolean registerValidator(javafx.scene.control.Control c,
Validator<T> validator)
Validator for specified control and makes control requiredc - control to validatevalidator - Validator to be usedpublic java.util.Set<javafx.scene.control.Control> getRegisteredControls()
public java.util.Optional<ValidationMessage> getHighestMessage(javafx.scene.control.Control target)
target - control