Modal Dialog not stop program flow

Hi
I am new to Vaadin Flow.
I have created a Modal Dialog to confirm whether the user is sure about their change, however the record is saved as the java logic continues even though the dialog is displayed.

Java/Swing the program flow would stop until the dialog is closed.

Am I doing something wrong?

Hi @pjc158, opening a modal dialog restricts interaction to just this dialog but doesn’t pause the server-side program. For what you describe, consider using Confirm Dialog in which you can subscribe to the confirm action and specify what to do next when the user confirms or cancels the action. For example:

ConfirmDialog dialog = new ConfirmDialog();
dialog.setHeader("Unsaved changes");
dialog.setText("There are unsaved changes. Do you want to discard or save them?");

dialog.setCancelable(true);
dialog.addCancelListener(event -> {
    // By default it just closes the dialog
});

dialog.setConfirmText("Save");
dialog.addConfirmListener(event -> {
    // Save the record here
});

Please refer to the documentation to learn more about Confirm Dialog.

Hi
That’s a bit of a pain, as I need to validate at least 10 fields (or more for some records), if the validation fails for one or more I do not want to save the record as the user has to correct the issue first.

I’m not sure what you’re trying to do.

You need to run the save function on the Confirm Listener.

I would probably validate the record before opening the Dialog and show the errors.

Maybe you have a code sample that would help us to understand.

Since you’re coming from Swing you can also read this: Tackling blocking dialogs in web applications with Vaadin | Vaadin

Hi
To keep it simple I have decided to add change listener to those fields that needs validating and show a config dialog, and then validate the rest of the fields at the end before saving the record.

However if I find an issue ask them to confirm the change and if they say no how do I force Vaadin to not leave the field or force it back to that field?

termsDays_NUMF.addValueChangeListener(event → {
if (event.getValue() != null) {
if (creditTerms_FF.creditTerms_FM != null) {
if (creditTerms_FF.creditTerms_FM.validate_termsDays() == false) {
// re-input value code here
}
}
}
});

You are making your life harder by manually doing all of that. Please take a look at the Binder concept and how normally validation should be handled. Binding Data to Forms | Data Binding | Vaadin Docs / Validating & Converting User Input | Data Binding | Vaadin Docs

1 Like

This is a fundamental difference between single threaded desktop application (Swing) and distributed system of the web application (Server and Browser are different machine). Hence the web applications follow event driven paradigm.

Your question is quite frequently popping up, and is something that requires some un-learning and re-learning. Respectively it means doing differently when converting Swing app to Web application.

Hi
We do not use beans, so binder will not work for us. We have our own data management layer.

Is there a UI call that allows us to reset focus on the field we are in if we get an error in validation?

You can call .focus() on a field.

It depends what we mean by “bean”. Binder is not restricted to “beans” like data base entities. It can be used with any POJO or even with a Map.

See example here: How do I use HashMap in Grid and Binder instead of POJO - Vaadin Cookbook

1 Like