2

I want to do some validation in JavaFx using ControlsFX. my code is like this:

ValidationSupport support = new ValidationSupport();
boolean isValid = true;
if(textField.getText().trim().isEmpty()) isValid = false;
support.registerValidator(testField, Validator.createEmptyValidator("This field is required!"));

My question is if is it possible to omit the if condition and extract whether or not the textField is empty from the validation support

Ayoub.A
  • 1,943
  • 3
  • 27
  • 34

1 Answers1

9

Your isValid variable and the if statement aren't really doing anything. The ValidationSupport contains an observable invalid property with which you can register listeners:

support.invalidProperty().addListener((obs, wasInvalid, isNowInvalid) -> {
    if (isNowInvalid) {
        System.out.println("Invalid");
    } else {
        System.out.println("Valid");
    }
});

or (perhaps more conveniently) register bindings:

Button okButton = new Button("OK");
okButton.disableProperty().bind(support.invalidProperty());

This last code snippet will ensure the okButton is only enabled if the text field is not empty.

James_D
  • 201,275
  • 16
  • 291
  • 322
  • how to apply this if I have to many TextFields to validate (my if condition was made so I could prevent a dialog window from closing) – Ayoub.A Dec 19 '16 at 19:49
  • Just register a validator with `support` for each of the text fields. – James_D Dec 19 '16 at 19:51