However when closing the dialog editor with Cancel button and a call to “closeWithDiscard”, the focus loss validation is triggered and the validation error message is shown and editor window is not closed.
With ESC key the dialog closes without triggering validation.
I would like to have the cancel button action similar to the ESC key. Any idea how can I do that?
Could you share screen controller and descriptor? I tried to reproduce your case but focus loss validation does not prevent closing a dialog by cancel button. My code:
The problem is when you show the validation error on focus loss, I give the focus back to the field that has the validation error. So the click on cancel button doesn’t even happen because of the refocus, however the ESC keypress works.
Yes, refocusing the field by blur event you cannot close the screen by mouse. In this case, additionally to ESC, you can add an action with some shortcut that will close the screen:
@Subscribe
public void onInit(InitEvent event) {
getWindow().addAction(
new BaseAction("customClose")
.withHandler(actionPerformedEvent -> closeWithDiscard())
.withShortcut("CTRL-I"));
}
Or in XML actions tag.
Another way is do not refocus field. You can set TextChangeEventMode to EAGER or LAZY with some delay and validate field in the ValueChangeEvent.