View Validation

Data validation in UI views is performed by the ViewValidation bean. It contains methods for validating UI components, displaying validation errors and unsaved changes dialogs. It is used by views and dialogs of the framework and add-ons. You can use it in the application views as well.

Below we describe the main methods of the ViewValidation bean.

validateUiComponents

The validateUiComponents() method accepts a collection of components or a component container. It runs validation for all passed components implementing the SupportsValidation interface and returns the ValidationErrors object which contains validation errors found for the validated components.

You can use this method in a view as follows:

public class SampleView extends StandardView {

    @Autowired
    private ViewValidation viewValidation;

    @ViewComponent
    private FormLayout form;

    @Subscribe("validateBtn")
    public void onValidateBtnClick(final ClickEvent<JmixButton> event) {
        ValidationErrors errors = viewValidation.validateUiComponents(form);
        if (!errors.isEmpty()) {
            viewValidation.showValidationErrors(errors);
        }
    }
}

showValidationErrors

The showValidationErrors() method shows a notification with validation errors, if the passed ValidationErrors object is not empty. See the usage example above.

The notification title is defined by the message with the validationFail.title key. You can override the default message by specifying a message with this key in your project’s message bundle, for example:

messages_en.properties
validationFail.title = Validation failed

showSaveConfirmationDialog

The showSaveConfirmationDialog() shows a confirmation dialog about saving or discarding unsaved changes. It is used by entity detail views when the user closes the view with unsaved changes and jmix.ui.view.use-save-confirmation property is set to true (which is the default).

The dialog uses messages with the following keys:

  • dialogs.closeUnsaved.title - the dialog header

  • dialogs.saveUnsaved.message - the dialog text

  • dialogs.closeUnsaved.save - "save" button text

  • dialogs.closeUnsaved.discard - "discard" button text

You can override the default messages by specifying messages with the same keys in your project’s message bundle, for example:

messages_en.properties
dialogs.closeUnsaved.title = You have unsaved changes
dialogs.saveUnsaved.message = Do you want to save changes before close?
dialogs.closeUnsaved.save = Save
dialogs.closeUnsaved.discard = Don't save

showUnsavedChangesDialog

The showUnsavedChangesDialog() shows a confirmation dialog about discarding unsaved changes. It is used by entity detail views when the user closes the view with unsaved changes and jmix.ui.view.use-save-confirmation property is set to false.

The dialog uses messages with the following keys:

  • dialogs.closeUnsaved.title - the dialog header

  • dialogs.closeUnsaved.message - the dialog text

You can override the default messages by specifying messages with the same keys in your project’s message bundle, for example:

messages_en.properties
dialogs.closeUnsaved.title = You have unsaved changes
dialogs.closeUnsaved.message = Do you want to discard unsaved changes?