- java.lang.Object
-
- org.controlsfx.validation.ValidationSupport
-
public class ValidationSupport extends Object
Provides validation support for UI components. The idea is create an instance of this class the component group, usually a panel.
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: aPredicate
to check the applicability of the control and aCallback
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
Properties Type Property Description BooleanProperty
errorDecorationEnabled
ReadOnlyBooleanProperty
invalid
Validation state propertyObjectProperty<ValidationDecoration>
validationDecorator
ReadOnlyObjectProperty<ValidationResult>
validationResult
Can be used to track validation result changes
-
Constructor Summary
Constructors Constructor Description ValidationSupport()
Creates validation support instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description BooleanProperty
errorDecorationEnabledProperty()
Optional<ValidationMessage>
getHighestMessage(Control target)
Returns optional highest severity message for a controlSet<Control>
getRegisteredControls()
Returns currently registered controlsValidationDecoration
getValidationDecorator()
Returns current validation decoratorValidationResult
getValidationResult()
Retrieves current validation resultvoid
initInitialDecoration()
Activates the initial decoration of validated controls.ReadOnlyBooleanProperty
invalidProperty()
Validation state propertyBoolean
isInvalid()
Returns current validation state.static boolean
isRequired(Control c)
Check control's required flagvoid
redecorate()
Redecorates all known components Only decorations related to validation are affected<T> boolean
registerValidator(Control c, boolean required, Validator<T> validator)
RegistersValidator
for specified control with additional possiblity to mark control as required or not.<T> boolean
registerValidator(Control c, Validator<T> validator)
RegistersValidator
for specified control and makes control requiredvoid
setErrorDecorationEnabled(boolean enabled)
Sets the value of the property errorDecorationEnabled.static void
setRequired(Control c, boolean required)
Set control's required flagvoid
setValidationDecorator(ValidationDecoration decorator)
Sets new validation decoratorObjectProperty<ValidationDecoration>
validationDecoratorProperty()
ReadOnlyObjectProperty<ValidationResult>
validationResultProperty()
Can be used to track validation result changes
-
-
-
Property Detail
-
errorDecorationEnabled
public BooleanProperty errorDecorationEnabledProperty
- See Also:
setErrorDecorationEnabled(boolean)
-
validationResult
public ReadOnlyObjectProperty<ValidationResult> validationResultProperty
Can be used to track validation result changes- See Also:
getValidationResult()
-
invalid
public ReadOnlyBooleanProperty invalidProperty
Validation state property- See Also:
isInvalid()
-
validationDecorator
public ObjectProperty<ValidationDecoration> validationDecoratorProperty
-
-
Constructor Detail
-
ValidationSupport
public ValidationSupport()
Creates validation support instance.
If initial decoration is desired invokeinitInitialDecoration()
.
-
-
Method Detail
-
setRequired
public static void setRequired(Control c, boolean required)
Set control's required flag- Parameters:
c
- controlrequired
- flag
-
isRequired
public static boolean isRequired(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
-
errorDecorationEnabledProperty
public BooleanProperty errorDecorationEnabledProperty()
- See Also:
setErrorDecorationEnabled(boolean)
-
setErrorDecorationEnabled
public void setErrorDecorationEnabled(boolean enabled)
Sets the value of the property errorDecorationEnabled.- Property description:
-
getValidationResult
public ValidationResult getValidationResult()
Retrieves current validation result- Returns:
- validation result
-
validationResultProperty
public ReadOnlyObjectProperty<ValidationResult> validationResultProperty()
Can be used to track validation result changes- See Also:
getValidationResult()
-
isInvalid
public Boolean isInvalid()
Returns current validation state.- Returns:
- true if there is at least one error
-
invalidProperty
public ReadOnlyBooleanProperty invalidProperty()
Validation state property- See Also:
isInvalid()
-
validationDecoratorProperty
public ObjectProperty<ValidationDecoration> validationDecoratorProperty()
-
getValidationDecorator
public ValidationDecoration getValidationDecorator()
Returns current validation decorator- Returns:
- current validation decorator or null if none
-
setValidationDecorator
public void setValidationDecorator(ValidationDecoration decorator)
Sets new validation decorator- Parameters:
decorator
- new validation decorator. Null value is valid - no decoration will occur
-
registerValidator
public <T> boolean registerValidator(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
public <T> boolean registerValidator(Control c, Validator<T> validator)
RegistersValidator
for specified control and makes control required- Parameters:
c
- control to validatevalidator
-Validator
to be used- Returns:
- true if registration is successful
-
getRegisteredControls
public Set<Control> getRegisteredControls()
Returns currently registered controls- Returns:
- set of currently registered controls
-
getHighestMessage
public Optional<ValidationMessage> getHighestMessage(Control target)
Returns optional highest severity message for a control- Parameters:
target
- control- Returns:
- Optional highest severity message for a control
-
-