Cancel Dialog without OnBlurNotifier Validation

Hi all,

I am trying to use field validation on focus loss using the following approach:

@Autowired
TextField<String> field;

field.unwrap(FieldEvents.BlurNotifier.class).addBlurListener(l -> {
      try {
          field.validate
      } catch(ValidationException e) {
          notifications.create().withCaption("Validation error:").show();
      }
});

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?

Thanks,
Samy

Hello!

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:

public class OrderDialogEdit extends StandardEditor<Order> {

    @Autowired
    private TextField<String> numberField;
    @Autowired
    private Notifications notifications;

    @Subscribe
    public void onInit(InitEvent event) {
        numberField.addValidator(value -> {
            throw new ValidationException("Validation error");
        });
        numberField.unwrap(FieldEvents.BlurNotifier.class).addBlurListener(l -> {
            try {
                numberField.validate();
            } catch (ValidationException e) {
                notifications.create().withCaption("Validation error:").show();
            }
        });
    }
}

I think the only way to avoid triggering focus loss validation is to use a shortcut combination for close action.

Hi Roman,

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.

I attached a project sample.

Thanks for your help.

focus-validation.zip (87.3 KB)

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.

1 Like