1

I'm using JFoenix to create a login form, my form do have a text field and a Password Field as you see validation is okey :)

What I'm trying to do, is to disable/enable automatically the "Connecter" button according to the validation of the two field

below my code

RequiredFieldValidator requiredFieldValidator ;
private void setupValidation() {
    requiredFieldValidator = new RequiredFieldValidator();
    requiredFieldValidator.setIcon(new ImageView(getClass().getResource("/icons/errors/invalid.png").toString()));
    requiredFieldValidator.setMessage("champs obligatoire");

    userName.getValidators().add(requiredFieldValidator);
    userName.focusedProperty().addListener((ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) -> {
        if (!newValue) {
            userName.validate();
        }
    });

    requiredFieldValidator = new RequiredFieldValidator();
    requiredFieldValidator.setIcon(new ImageView(getClass().getResource("/icons/errors/invalid.png").toString()));
    requiredFieldValidator.setMessage("champs obligatoire");
    password.getValidators().add(requiredFieldValidator);
    password.focusedProperty().addListener((ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) -> {
        if (!newValue) {
            password.validate();
        }
    });
    requiredFieldValidator = new RequiredFieldValidator();
    requiredFieldValidator.setIcon(new ImageView(getClass().getResource("/icons/errors/invalid.png").toString()));
    requiredFieldValidator.setMessage("champs obligatoire");
    passwordConfirm.getValidators().add(requiredFieldValidator);
    passwordConfirm.focusedProperty().addListener((ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) -> {
        if (!newValue) {
            passwordConfirm.validate();
        }
    });

}

If you believe that there is a better way to do so, I'm open to any suggestion.

Thanks.

2 Answers2

3

In javafx we can bind easily between controls properties as visibility or inability. All I did was binding the disableProperty of my connect button to the textProperty of all my required fields, and that pretty simple using logical expressions.

my added code is bellow :

    connect.disableProperty().bind((
            passwordConfirm.textProperty().isNotEmpty().and(
            password.textProperty().isNotEmpty()).and(
            userName.textProperty().isNotEmpty())
            ).not());

this answer is not using the validation of JFoenix as I wished, but is some how satisfying my needs.

2

You can use the JFoenix validation, as you wished initially. When you call the validate() method, you can actually obtain a boolean, which will be true/false depending on the validation, each time that the focused property changes. That is:

userName.focusedProperty().addListener((ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) -> {
    if (!newValue) {
        if(userName.validate()) userName.setDisable(false);
        else userName.setDisable(true);
    }
});