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.
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).
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.
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
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.
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.
Add listeners to your TextField
s, 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With