java.lang.Object
org.controlsfx.validation.ValidationSupport
Provides validation support for UI components. The idea is create an instance of this class the component group, usually a panel.
Once created,
Once created,
Validator
s 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());
-
Property Summary
PropertiesTypePropertyDescriptionjavafx.beans.property.BooleanProperty
javafx.beans.property.ReadOnlyBooleanProperty
Validation state propertyjavafx.beans.property.ObjectProperty
<ValidationDecoration> javafx.beans.property.ReadOnlyObjectProperty
<ValidationResult> Can be used to track validation result changes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionjavafx.beans.property.BooleanProperty
getHighestMessage
(javafx.scene.control.Control target) Returns optional highest severity message for a controlSet
<javafx.scene.control.Control> Returns currently registered controlsReturns current validation decoratorRetrieves current validation resultvoid
Activates the initial decoration of validated controls.javafx.beans.property.ReadOnlyBooleanProperty
Validation state propertyReturns current validation state.static boolean
isRequired
(javafx.scene.control.Control c) Check control's required flagvoid
Redecorates all known components Only decorations related to validation are affected<T> boolean
registerValidator
(javafx.scene.control.Control c, boolean required, Validator<T> validator) RegistersValidator
for specified control with additional possiblity to mark control as required or not.<T> boolean
registerValidator
(javafx.scene.control.Control c, Validator<T> validator) RegistersValidator
for specified control and makes control requiredvoid
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 theerrorDecorationEnabled
property.static void
setRequired
(javafx.scene.control.Control c, boolean required) Set control's required flagvoid
setValidationDecorator
(ValidationDecoration decorator) Sets new validation decoratorjavafx.beans.property.ObjectProperty
<ValidationDecoration> javafx.beans.property.ReadOnlyObjectProperty
<ValidationResult> Can be used to track validation result changes
-
Property Details
-
errorDecorationEnabled
public javafx.beans.property.BooleanProperty errorDecorationEnabledProperty- See Also:
-
validationResult
Can be used to track validation result changes- See Also:
-
invalid
public javafx.beans.property.ReadOnlyBooleanProperty invalidPropertyValidation state property- See Also:
-
validationDecorator
- See Also:
-
-
Constructor Details
-
ValidationSupport
public ValidationSupport()Creates validation support instance.
If initial decoration is desired invokeinitInitialDecoration()
.
-
-
Method Details
-
setRequired
public static void setRequired(javafx.scene.control.Control c, boolean required) Set control's required flag- Parameters:
c
- controlrequired
- flag
-
isRequired
public static boolean isRequired(javafx.scene.control.Control c) Check control's required flag- Parameters:
c
- control- Returns:
- true if required
-
initInitialDecoration
public void initInitialDecoration()Activates the initial decoration of validated controls.
By default the decoration will only be applied after the first change of one validated controls value. -
redecorate
public void redecorate()Redecorates all known components Only decorations related to validation are affected -
revalidate
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
public void revalidate(javafx.scene.control.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. -
errorDecorationEnabledProperty
public javafx.beans.property.BooleanProperty errorDecorationEnabledProperty()- Returns:
- the
errorDecorationEnabled
property - See Also:
-
setErrorDecorationEnabled
public void setErrorDecorationEnabled(boolean enabled) Sets the value of theerrorDecorationEnabled
property.- Property description:
- Parameters:
enabled
- the value for theerrorDecorationEnabled
property- See Also:
-
getValidationResult
Retrieves current validation result- Returns:
- validation result
-
validationResultProperty
Can be used to track validation result changes- Returns:
- The Validation result property.
- See Also:
-
isInvalid
Returns current validation state.- Returns:
- true if there is at least one error
-
invalidProperty
public javafx.beans.property.ReadOnlyBooleanProperty invalidProperty()Validation state property- Returns:
- validation state property
- See Also:
-
validationDecoratorProperty
- Returns:
- The Validation decorator property
- See Also:
-
getValidationDecorator
Returns current validation decorator- Returns:
- current validation decorator or null if none
-
setValidationDecorator
Sets new validation decorator- Parameters:
decorator
- new validation decorator. Null value is valid - no decoration will occur
-
registerValidator
public <T> boolean registerValidator(javafx.scene.control.Control c, boolean required, Validator<T> validator) RegistersValidator
for specified control with additional possiblity to mark control as required or not.- Parameters:
c
- control to validaterequired
- true if controls should be requiredvalidator
-Validator
to be used- Returns:
- true if registration is successful
-
registerValidator
RegistersValidator
for specified control and makes control required- Parameters:
c
- control to validatevalidator
-Validator
to be used- Returns:
- true if registration is successful
-
getRegisteredControls
Returns currently registered controls- Returns:
- set of currently registered controls
-
getHighestMessage
Returns optional highest severity message for a control- Parameters:
target
- control- Returns:
- Optional highest severity message for a control
-