Module MaterialFX

Class MFXValidator

java.lang.Object
io.github.palexdev.materialfx.validation.MFXValidator

public class MFXValidator extends Object
A basic implementation of a validator in JavaFX.

This validator allows to specify the conditions to be met as Constraints, and also allows to add other MFXValidators as dependencies, meaning that the validator will be valid only when all its constraints are valid and all its dependencies are also valid.

You can track every single constraint change by defining the setOnUpdated(BiConsumer) action performed when the update() method is triggered.

You have two ways of querying the validator's state:

One is to simply query the validProperty()

The other is to call validate()

  • Property Details

  • Constructor Details

    • MFXValidator

      public MFXValidator()
  • Method Details

    • constraint

      public MFXValidator constraint(Constraint constraint)
      Adds the given Constraint to the validator's constraint list.

      Also adds an InvalidationListener to the constraint's condition to trigger the update() method when it changes. This is needed to automatically update the validProperty().

      The listener is build using the new When construct.
    • constraint

      public MFXValidator constraint(Severity severity, String message, BooleanExpression condition)
      Creates a Constraint with the given parameters, then calls constraint(Constraint).
    • constraint

      public MFXValidator constraint(String message, BooleanExpression condition)
      Creates a Constraint with ERROR severity and the given message and condition, then calls constraint(Constraint).
    • removeConstraint

      public MFXValidator removeConstraint(Constraint constraint)
      Removes the given Constraint from the validator.

      Also invokes When.disposeFor(ObservableValue) on the constraint's condition (see constraint(Constraint) and When).

    • dependsOn

      public MFXValidator dependsOn(MFXValidator validator)
      Adds the given MFXValidator dependency to this validator.

      Also adds an InvalidationListener to the dependency validProperty() to trigger the update() method when it changes. This is needed to automatically update the validProperty()

    • removeDependency

      public MFXValidator removeDependency(MFXValidator validator)
      Removes the given validator dependency from this validator.

      Also calls When.disposeFor(ObservableValue) on the dependency's valid property (see dependsOn(MFXValidator) and When).

    • validate

      public List<Constraint> validate()
      This method queries all the validator's dependencies and constraints to build a list containing all the unmet constraints.

      If the list is not empty then the validator' state is invalid.

      The list can also be sorted by constraint severity by setting setSortBySeverity(boolean) to true.

      The method can be also set to "fail fast" meaning that we do not care about all the invalid conditions but it's also enough to get the first one. This applies to both dependencies and constraints. In this case the sorting is ignored of course since the list will always contain at most one constraint.
    • update

      public void update()
      This is the method responsible for updating the validator' state. Despite being public it should not be necessary to call it automatically as the constraints and the dependencies automatically trigger this method.

      Note that constraints are evaluated in order of insertion and according to their Constraint.getChainMode(), so be careful with OR modes.

      At the end invokes onUpdated().
    • validateToString

      public String validateToString()
      Calls validate() then chains all the invalid constraints' messages into a String.
    • onUpdated

      protected void onUpdated()
      This is called when the update() method is triggered and it's responsible for running the action specified by the user, setOnUpdated(BiConsumer).
    • isValid

      public boolean isValid()
      Returns:
      whether the validator' state is valid
    • validProperty

      public ReadOnlyBooleanProperty validProperty()
      Specifies the validator' state. This is given by chaining all the validator's dependencies and constraints.
      See Also:
    • setValid

      protected void setValid(boolean valid)
      Sets the value of the property valid.
      Property description:
      Specifies the validator' state. This is given by chaining all the validator's dependencies and constraints.
    • getOnUpdated

      public BiConsumer<Boolean,List<Constraint>> getOnUpdated()
      Returns:
      the action to perform after an update()
      See Also:
    • setOnUpdated

      public MFXValidator setOnUpdated(BiConsumer<Boolean,List<Constraint>> onUpdated)
      Allows to specify the action to perform every time the update() method is triggered. The action is a BiConsumer carrying the validator' state and the list of invalid constraints (empty if valid of course).
    • isSortBySeverity

      public boolean isSortBySeverity()
      Returns:
      whether the invalid constraints list is sorted by severity
    • setSortBySeverity

      public MFXValidator setSortBySeverity(boolean sortBySeverity)
      Allows to specify whether to sort the invalid constraints list by severity when calling validate().
    • isFailFast

      public boolean isFailFast()
      Returns:
      whether the validate() method should fail fast
    • setFailFast

      public MFXValidator setFailFast(boolean failFast)
      Sets whether the validate() method should fail fast.