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());
| Modifier and Type | Field and Description |
|---|---|
| private final ObservableSet | |
| private static final String | |
| private final AtomicBoolean | |
| private BooleanProperty | |
| private BooleanProperty | |
| private ObjectProperty | |
| private ReadOnlyObjectWrapper | |
| private final ObservableMap |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public BooleanProperty | |
| public Optional | Returns: Optional highest severity message for a controlcontrol target)Returns optional highest severity message for a control |
| public Set | Returns: set of currently registered controlsReturns currently registered controls |
| public ValidationDecoration | Returns: current validation decorator or null if noneReturns current validation decorator |
| public ValidationResult | |
| public void | |
| public ReadOnlyBooleanProperty | |
| private boolean | |
| public Boolean | |
| public static boolean | |
| public void | |
| public <T> boolean | Returns: true if registration is successfulcontrol to validate c, boolean true if controls should be required required, final Validator<T> Validator to be usedRegisters |
| public <T> boolean | Returns: true if registration is successfulcontrol to validate c, final Validator<T> Validator to be usedRegisters |
| public void | |
| public void | |
| public void | |
| public static void | |
| public void | setValidationDecorator(ValidationDecoration
new validation decorator. Null value is valid - no decoration will occur decorator)Sets new validation decorator |
| public ObjectProperty | |
| public ReadOnlyObjectProperty | Returns: The Validation result property.Can be used to track validation result changes |
| controls | back to summary |
|---|---|
| private final ObservableSet<Control> controls | |
| CTRL_REQUIRED_FLAG | back to summary |
|---|---|
| private static final String CTRL_REQUIRED_FLAG | |
| dataChanged | back to summary |
|---|---|
| private final AtomicBoolean dataChanged | |
| errorDecorationEnabledProperty | back to summary |
|---|---|
| private BooleanProperty errorDecorationEnabledProperty | |
| invalidProperty | back to summary |
|---|---|
| private BooleanProperty invalidProperty | |
| validationDecoratorProperty | back to summary |
|---|---|
| private ObjectProperty<ValidationDecoration> validationDecoratorProperty | |
| validationResultProperty | back to summary |
|---|---|
| private ReadOnlyObjectWrapper<ValidationResult> validationResultProperty | |
| validationResults | back to summary |
|---|---|
| private final ObservableMap<Control, ValidationResult> validationResults | |
| ValidationSupport | back to summary |
|---|---|
| public ValidationSupport() Creates validation support instance. | |
| errorDecorationEnabledProperty | back to summary |
|---|---|
| public BooleanProperty errorDecorationEnabledProperty() | |
| getHighestMessage | back to summary |
|---|---|
| public Optional Returns optional highest severity message for a control
| |
| getRegisteredControls | back to summary |
|---|---|
| public Set Returns currently registered controls | |
| getValidationDecorator | back to summary |
|---|---|
| public ValidationDecoration getValidationDecorator() Returns current validation decorator
| |
| getValidationResult | back to summary |
|---|---|
| public ValidationResult getValidationResult() Retrieves current validation result
| |
| initInitialDecoration | back to summary |
|---|---|
| public void initInitialDecoration() Activates the initial decoration of validated controls. | |
| invalidProperty | back to summary |
|---|---|
| public ReadOnlyBooleanProperty invalidProperty() Validation state property
| |
| isErrorDecorationEnabled | back to summary |
|---|---|
| private boolean isErrorDecorationEnabled() | |
| isInvalid | back to summary |
|---|---|
| public Boolean isInvalid() Returns current validation state.
| |
| isRequired | back to summary |
|---|---|
| public static boolean isRequired(Control c) Check control's required flag
| |
| redecorate | back to summary |
|---|---|
| public void redecorate() Redecorates all known components Only decorations related to validation are affected | |
| registerValidator | back to summary |
|---|---|
| public <T> boolean registerValidator(final Control c, boolean required, final Validator<T> validator) Registers
| |
| registerValidator | back to summary |
|---|---|
| public <T> boolean registerValidator(final Control c, final Validator<T> validator) Registers | |
| revalidate | back to summary |
|---|---|
| public void revalidate() Triggers validation for all known components. It is only necessary to call this if it is needed to revalidate even if the value of the control has not changed. | |
| revalidate | back to summary |
|---|---|
| public void revalidate(Control c) Triggers validation for the given component. It is only necessary to call this if it is needed to revalidate even if the value of the control has not changed. | |
| setErrorDecorationEnabled | back to summary |
|---|---|
| public void setErrorDecorationEnabled(boolean enabled) | |
| setRequired | back to summary |
|---|---|
| public static void setRequired(Control c, boolean required) Set control's required flag
| |
| setValidationDecorator | back to summary |
|---|---|
| public void setValidationDecorator(ValidationDecoration decorator) Sets new validation decorator
| |
| validationDecoratorProperty | back to summary |
|---|---|
public ObjectProperty
| |
| validationResultProperty | back to summary |
|---|---|
| public ReadOnlyObjectProperty Can be used to track validation result changes
| |