Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep showing a dialog when validation of input fails

I currently have a dialog with four TextFields from which the input is collected. When the input can not meet my requirements, the dialog should stay open and show an error message. I currently have the following code:

dlg.setResultConverter(dialogButton -> {

    if (dialogButton == createButtonType) {

        ArrayList<String> returnList = getInput();

        if (returnList != null) {
            return returnList;
        }
        else {
            dlg.showAndWait();
        }

    }
    return null;
});

Optional<ArrayList<String>> result = dlg.showAndWait();
result.ifPresent(customerDetails -> {

    //Use the input for something

});

The getInput() method gets the text from the TextFields and tests it against some requirements. When the input passes, the four Strings are put in an ArrayList<String>. Else, the method returns null. As you can see, the result converter checks if the method returned null. If it didn't, it just returns the ArrayList<String>.

But, when the validation fails, the dialog should be kept open. It works with dlg.showAndWait(), but the downside is that it triggers an error: java.lang.IllegalStateException: Stage already visible. (it keeps working, but getting an error is not the way it should be)

My question is: does anyone know how to do this, without triggering an error?

Note: I am using JavaFX 8 (JDK 1.8.0_40), which has the dialog features built in.

like image 282
bashoogzaad Avatar asked Dec 17 '14 10:12

bashoogzaad


People also ask

When should input validation techniques be used?

Furthermore, input validation techniques should happen as early as possible in the data flow, preferably as soon as the data is received from the external party. Input Validation can be applied on two levels: Syntactic validation, which checks the proper syntax of structured fields (SSN, date, currency symbol).

Do you have a validation error on your website?

For proof, just look at the sites showcased in CSS galleries, 90% will have validation errors – Most of which are easy and simple fixes. Let’s look at some of the most common validation errors that appear time and time again, and how to correct them to really finish off your sites with high-quality code.

Why server-side validation is important for your website?

However, server-side validation is a required feature of any decent web application because it prevents a lot of potentially harmful scenarios, such as the following: Implementation errors of the client-side validation process, which can fail to block badly-formatted data

Can input validation prevent SQL injection and other attacks?

OWASP advises that, although Input Validation techniques should not be used as the primary method of preventing XSS, SQL Injection and other attacks, they can significantly contribute to reducing their impact.


2 Answers

final Button okButton = (Button) dialog.getDialogPane().lookupButton(ButtonType.OK);
okButton.addEventFilter(ActionEvent.ACTION, ae -> {
    if (!isValid()) {
        ae.consume(); //not valid
    }
});

Works fine for me. Just use filter as it fires first.

BTW it is described in official docs.

like image 188
Dave_cz Avatar answered Oct 13 '22 00:10

Dave_cz


Add listeners to your TextFields, validating the user input.

If the user input is valid, enable the "OK" Button (or whatever its name is) if the user input is invalid, disable the Button.

If the Button for closing the Dialog is disabled, the only way to close the Dialog is to cancel, which results in an empty Optional as result.

like image 35
eckig Avatar answered Oct 13 '22 01:10

eckig