Class Validator

java.lang.Object
com.dua3.utility.fx.controls.Validator

public class Validator extends Object
The Validator class is used for validating controls in a JavaFX application. It provides methods for setting validation rules for different types of controls, such as TextInputControl and Control. It also allows for custom validation rules to be set.

The Validator class supports the use of a ResourceBundle for looking up message texts. If a ResourceBundle is provided during initialization, it will be used to retrieve the messages for validation errors. Otherwise, the message will be used as is.

The Validator class also supports decorating nodes with invalid values. When a control fails validation, an icon and tooltip can be added to the control to indicate the validation error.

The Validator class is meant to be used in a JavaFX application and should be initialized with a valid ResourceBundle if localized validation messages are required.

  • Property Summary

    Properties
    Type
    Property
    Description
    javafx.beans.property.ReadOnlyBooleanProperty
    Returns the read-only boolean property indicating the validity of all controls.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a Validator instance without assigning a resource bundle.
    Validator(@Nullable ResourceBundle resources)
    ^ Creates a Validator instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addCheck(javafx.scene.control.Control c, String message, BooleanSupplier test, javafx.beans.Observable... trigger)
    Custom validation.
    void
    addCheck(javafx.scene.control.Control c, String message, javafx.beans.value.ObservableValue<Boolean> test)
    Custom validation.
    void
    addValidation(@Nullable javafx.scene.control.Button button)
    Add validation for a button.
    void
    Remove all validation decorations from controls.
    void
    disallowEmpty(javafx.scene.control.TextInputControl c, String message)
    Add a rule that assures the control is not empty.
    void
    Run cleanup actions.
    void
    Focuses the first control.
    void
    Focuses the first control with an invalid validation result.
    boolean
    Check if decorations are enabled.
    void
    setDecorateNodes(boolean decorateNodes)
    Set to true to enable decorating nodes with invalid values.
    void
    setIconSize(int sz)
    Set icon size.
    void
    setRegex(javafx.scene.control.TextInputControl c, String message, String regex)
    Add a rule that assures the content matches a regular expression.
    void
    Validate all rules of this validator, update decorations, and update value of validProperty.
    javafx.beans.property.ReadOnlyBooleanProperty
    Returns the read-only boolean property indicating the validity of all controls.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Property Details

    • valid

      public javafx.beans.property.ReadOnlyBooleanProperty validProperty
      Returns the read-only boolean property indicating the validity of all controls.
      See Also:
  • Constructor Details

    • Validator

      public Validator()
      Creates a Validator instance without assigning a resource bundle.
    • Validator

      public Validator(@Nullable ResourceBundle resources)
      ^ Creates a Validator instance.
      Parameters:
      resources - the resource bundle to look up message texts
  • Method Details

    • dispose

      public void dispose()
      Run cleanup actions.
    • disallowEmpty

      public void disallowEmpty(javafx.scene.control.TextInputControl c, String message)
      Add a rule that assures the control is not empty.
      Parameters:
      c - the control
      message - the message to display if validation fails
    • setRegex

      public void setRegex(javafx.scene.control.TextInputControl c, String message, String regex)
      Add a rule that assures the content matches a regular expression.
      Parameters:
      c - the control
      message - the message to display if validation fails
      regex - the regular expression to test the control's text
    • addCheck

      public void addCheck(javafx.scene.control.Control c, String message, BooleanSupplier test, javafx.beans.Observable... trigger)
      Custom validation.
      Parameters:
      c - the control
      message - the message to display if validation fails
      test - the test to perform the validation
      trigger - the Observables that trigger validation
    • addCheck

      public void addCheck(javafx.scene.control.Control c, String message, javafx.beans.value.ObservableValue<Boolean> test)
      Custom validation.
      Parameters:
      c - the control
      message - the message to display if validation fails
      test - ObservableValue that triggers updates and provides the validation result
    • validateAll

      public void validateAll()
      Validate all rules of this validator, update decorations, and update value of validProperty.
    • clearDecorations

      public void clearDecorations()
      Remove all validation decorations from controls.
    • setDecorateNodes

      public void setDecorateNodes(boolean decorateNodes)
      Set to true to enable decorating nodes with invalid values.
      Parameters:
      decorateNodes - true, if decoration shall be added to controls with invalid values
    • isDecorateNodes

      public boolean isDecorateNodes()
      Check if decorations are enabled.
      Returns:
      true, if decorations are enabled
    • setIconSize

      public void setIconSize(int sz)
      Set icon size.
      Parameters:
      sz - the new size
    • addValidation

      public void addValidation(@Nullable javafx.scene.control.Button button)
      Add validation for a button. If validation is added to a button, the validation will be performed when the button is pressed, and if validation fails, the button clicked event will be consumed.
      Parameters:
      button - the button to add validation to
    • focusFirst

      public void focusFirst()
      Focuses the first control.
    • focusFirstInvalid

      public void focusFirstInvalid()
      Focuses the first control with an invalid validation result.
    • validProperty

      public javafx.beans.property.ReadOnlyBooleanProperty validProperty()
      Returns the read-only boolean property indicating the validity of all controls.
      Returns:
      the validProperty