Top Description Fields Constructors Methods
org.controlsfx.validation

public Class ValidationSupport

extends Object
Class Inheritance
Imports
java.util.Collections, .Optional, .Set, .WeakHashMap, java.util.concurrent.ConcurrentHashMap, java.util.concurrent.atomic.AtomicBoolean, java.util.function.Consumer, .Predicate, javafx.application.Platform, javafx.beans.property.BooleanProperty, .ObjectProperty, .ReadOnlyBooleanProperty, .ReadOnlyObjectProperty, .ReadOnlyObjectWrapper, .SimpleBooleanProperty, .SimpleObjectProperty, javafx.beans.value.ObservableValue, javafx.collections.FXCollections, .MapChangeListener, .ObservableMap, .ObservableSet, javafx.scene.control.Control, javafx.util.Callback, org.controlsfx.tools.ValueExtractor, org.controlsfx.validation.decoration.GraphicValidationDecoration, .ValidationDecoration

Provides validation support for UI components. The idea is create an instance of this class the component group, usually a panel.
Once created, 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());
 

Field Summary

Modifier and TypeField and Description
private final ObservableSet<Control>
private static final String
private final AtomicBoolean
private BooleanProperty
private BooleanProperty
private ObjectProperty<ValidationDecoration>
private ReadOnlyObjectWrapper<ValidationResult>
private final ObservableMap<Control, ValidationResult>

Constructor Summary

AccessConstructor and Description
public
ValidationSupport()

Creates validation support instance.

Method Summary

Modifier and TypeMethod and Description
public BooleanProperty
public Optional<ValidationMessage>

Returns:

Optional highest severity message for a control
getHighestMessage
(Control
control
target
)

Returns optional highest severity message for a control

public Set<Control>

Returns:

set of currently registered controls
getRegisteredControls
()

Returns currently registered controls

public ValidationDecoration

Returns:

current validation decorator or null if none
getValidationDecorator
()

Returns current validation decorator

public ValidationResult

Returns:

validation result
getValidationResult
()

Retrieves current validation result

public void
initInitialDecoration()

Activates the initial decoration of validated controls.

public ReadOnlyBooleanProperty

Returns:

validation state property
invalidProperty
()

Validation state property

private boolean
public Boolean

Returns:

true if there is at least one error
isInvalid
()

Returns current validation state.

public static boolean

Returns:

true if required
isRequired
(Control
control
c
)

Check control's required flag

public void
redecorate()

Redecorates all known components Only decorations related to validation are affected

public <T> boolean

Returns:

true if registration is successful
registerValidator
(final Control
control to validate
c
,
boolean
true if controls should be required
required
,
final Validator<T>
Validator to be used
validator
)

Registers Validator for specified control with additional possiblity to mark control as required or not.

public <T> boolean

Returns:

true if registration is successful
registerValidator
(final Control
control to validate
c
,
final Validator<T>
Validator to be used
validator
)

Registers Validator for specified control and makes control required

public void
revalidate()

Triggers validation for all known components.

public void
revalidate(Control c)

Triggers validation for the given component.

public void
setErrorDecorationEnabled(boolean enabled)

public static void
setRequired(Control
control
c
,
boolean
flag
required
)

Set control's required flag

public void
setValidationDecorator(ValidationDecoration
new validation decorator. Null value is valid - no decoration will occur
decorator
)

Sets new validation decorator

public ObjectProperty<ValidationDecoration>
validationDecoratorProperty()

Returns: The Validation decorator property

public ReadOnlyObjectProperty<ValidationResult>

Returns:

The Validation result property.
validationResultProperty
()

Can be used to track validation result changes

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

controlsback to summary
private final ObservableSet<Control> controls
CTRL_REQUIRED_FLAGback to summary
private static final String CTRL_REQUIRED_FLAG
dataChangedback to summary
private final AtomicBoolean dataChanged
errorDecorationEnabledPropertyback to summary
private BooleanProperty errorDecorationEnabledProperty
invalidPropertyback to summary
private BooleanProperty invalidProperty
validationDecoratorPropertyback to summary
private ObjectProperty<ValidationDecoration> validationDecoratorProperty
validationResultPropertyback to summary
private ReadOnlyObjectWrapper<ValidationResult> validationResultProperty
validationResultsback to summary
private final ObservableMap<Control, ValidationResult> validationResults

Constructor Detail

ValidationSupportback to summary
public ValidationSupport()

Creates validation support instance.
If initial decoration is desired invoke initInitialDecoration().

Method Detail

errorDecorationEnabledPropertyback to summary
public BooleanProperty errorDecorationEnabledProperty()
getHighestMessageback to summary
public Optional<ValidationMessage> getHighestMessage(Control target)

Returns optional highest severity message for a control

Parameters
target:Control

control

Returns:Optional<ValidationMessage>

Optional highest severity message for a control

getRegisteredControlsback to summary
public Set<Control> getRegisteredControls()

Returns currently registered controls

Returns:Set<Control>

set of currently registered controls

getValidationDecoratorback to summary
public ValidationDecoration getValidationDecorator()

Returns current validation decorator

Returns:ValidationDecoration

current validation decorator or null if none

getValidationResultback to summary
public ValidationResult getValidationResult()

Retrieves current validation result

Returns:ValidationResult

validation result

initInitialDecorationback to summary
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.

invalidPropertyback to summary
public ReadOnlyBooleanProperty invalidProperty()

Validation state property

Returns:ReadOnlyBooleanProperty

validation state property

isErrorDecorationEnabledback to summary
private boolean isErrorDecorationEnabled()
isInvalidback to summary
public Boolean isInvalid()

Returns current validation state.

Returns:Boolean

true if there is at least one error

isRequiredback to summary
public static boolean isRequired(Control c)

Check control's required flag

Parameters
c:Control

control

Returns:boolean

true if required

redecorateback to summary
public void redecorate()

Redecorates all known components Only decorations related to validation are affected

registerValidatorback to summary
public <T> boolean registerValidator(final Control c, boolean required, final Validator<T> validator)

Registers Validator for specified control with additional possiblity to mark control as required or not.

Parameters
c:Control

control to validate

required:boolean

true if controls should be required

validator:Validator<T>

Validator to be used

Returns:boolean

true if registration is successful

Annotations
@SuppressWarnings:unchecked
registerValidatorback to summary
public <T> boolean registerValidator(final Control c, final Validator<T> validator)

Registers Validator for specified control and makes control required

Parameters
c:Control

control to validate

validator:Validator<T>

Validator to be used

Returns:boolean

true if registration is successful

revalidateback 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.

revalidateback 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.

setErrorDecorationEnabledback to summary
public void setErrorDecorationEnabled(boolean enabled)
setRequiredback to summary
public static void setRequired(Control c, boolean required)

Set control's required flag

Parameters
c:Control

control

required:boolean

flag

setValidationDecoratorback to summary
public void setValidationDecorator(ValidationDecoration decorator)

Sets new validation decorator

Parameters
decorator:ValidationDecoration

new validation decorator. Null value is valid - no decoration will occur

validationDecoratorPropertyback to summary
public ObjectProperty<ValidationDecoration> validationDecoratorProperty()
Returns:ObjectProperty<ValidationDecoration>

The Validation decorator property

validationResultPropertyback to summary
public ReadOnlyObjectProperty<ValidationResult> validationResultProperty()

Can be used to track validation result changes

Returns:ReadOnlyObjectProperty<ValidationResult>

The Validation result property.