Event Handler in case of failing validations

Hi everyone,

today we ran into the following situation: We wanted to disable-on-click a button and used the jmix build-in way for doing this:

<button id="commitAndCloseBtn" action="windowCommitAndClose" disableOnClick="true"/>

Then we have two cases:

  • The operations on commit run successfully and the window is closed => This is fine
  • One of the validations fail before committing the operation and we need to re-renable the button, so the user is able to change the entered data and commit again

For the second point we only found the way to override the method closeWithCommit in the following way:

@Override
    public OperationResult closeWithCommit() {
        commitAndCloseBtn.setEnabled(true);
        return super.closeWithCommit();
    }

In the team we agreed, that the nicer way to do this re-enabling of the button would be to have an event handler which allows us to do something in case the validation fails either in StandardEditor.validateUiComponents or in StandardEditor.validateAdditionalRules().

Is there any event handler we missed here and which could be used to achieve what we need? Or could this be a possible improvement for a future jmix version?

Thank you for some ideas or discussions!

Best regards,
Thomas

Hi,

there is no dedicated event like ValidationFailedEvent, but you still have 2 options to achieve what you need:

// Option 1
@Override
public OperationResult closeWithCommit() {
    return super.closeWithCommit()
            .otherwise(() -> {
                commitAndCloseBtn.setEnabled(true);
            });
}

// Option 2
@Override
protected ValidationErrors validateScreen() {
    ValidationErrors validationErrors = super.validateScreen();

    if (!validationErrors.isEmpty()) {
        commitAndCloseBtn.setEnabled(true);
    }

    return validationErrors;
}

Regads,
Gleb

1 Like