Skip to content

Commit 935b990

Browse files
committed
Using ValidationEvent for additional validation of entity details screen #811
1 parent 9a16698 commit 935b990

2 files changed

Lines changed: 27 additions & 7 deletions

File tree

content/modules/flow-ui/pages/views/view-events.adoc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,19 @@ include::example$/onboarding/src/main/java/com/company/onboarding/view/user/User
131131
[[validation-event]]
132132
== ValidationEvent
133133

134-
`ValidationEvent` is fired in views inherited from `StandardDetailView` when the view is validated on saving the view xref:data/data-context.adoc[DataContext]. Use this event listener to perform additional validation of the view.
134+
`ValidationEvent` is fired in views inherited from `StandardDetailView` when the view is saved. You can use this event listener to perform custom validation of the view.
135+
136+
To report validation errors and abort the save process, use the `ValidationErrors` object returned by the `getErrors()` method of the event. For example:
135137

136138
[source,java,indent=0]
137139
----
138140
include::example$/onboarding/src/main/java/com/company/onboarding/view/user/UserDetailEventsView.java[tags=validation-event]
139141
----
140142

143+
If the `ValidationErrors` object is not empty after the validation process, the saving operation will not be executed, and the view will display errors using the `ViewValidation.showValidationErrors()` method.
144+
145+
See xref:views/view-validation.adoc[] for more information.
146+
141147
[[before-save-vent]]
142148
== BeforeSaveEvent
143149

content/modules/flow-ui/pages/views/view-validation.adoc

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,38 @@
11
= View Validation
22
:page-aliases: screens/validation.adoc
33

4-
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.
4+
[[validation-steps]]
5+
== Validation Steps
6+
7+
Before the detail view saves data during the execution of `closeWithSave()` and `save()` xref:views/view-methods.adoc#methods-of-standard-detail[methods], it validates the data in the following order:
8+
9+
. The view invokes the `validateUiComponents()` method of the `ViewValidation` bean described <<validateUiComponents,below>>.
10+
11+
. If there were no errors on the first step, the view sends the xref:views/view-events.adoc#validation-event[ValidationEvent] event, that can be handled in the view controller for additional validation.
12+
13+
[[view-validation-bean]]
14+
== ViewValidation Bean
15+
16+
The `ViewValidation` bean 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.
517

618
Below we describe the main methods of the `ViewValidation` bean.
719

820
[[validateUiComponents]]
9-
== validateUiComponents
21+
=== validateUiComponents
1022

1123
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.
1224

13-
You can use this method in a view as follows:
25+
This method is called by detail views during the execution of `closeWithSave()` and `save()` methods.
26+
27+
You can call it also in response to user actions, for example:
1428

1529
[source,java,indent=0]
1630
----
1731
include::example$/onboarding/src/main/java/com/company/onboarding/view/validation/SampleView.java[tags=validateUiComponents]
1832
----
1933

2034
[[showValidationErrors]]
21-
== showValidationErrors
35+
=== showValidationErrors
2236

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

@@ -31,7 +45,7 @@ validationFail.title = Validation failed
3145
----
3246

3347
[[showSaveConfirmationDialog]]
34-
== showSaveConfirmationDialog
48+
=== showSaveConfirmationDialog
3549

3650
The `showSaveConfirmationDialog()` shows a confirmation dialog about saving or discarding unsaved changes. It is used by xref:views/views.adoc#standard-detail-view[entity detail views] when the user closes the view with unsaved changes and xref:ui-properties.adoc#jmix.ui.view.use-save-confirmation[jmix.ui.view.use-save-confirmation] property is set to `true` (which is the default).
3751

@@ -54,7 +68,7 @@ dialogs.closeUnsaved.discard = Don't save
5468
----
5569

5670
[[showUnsavedChangesDialog]]
57-
== showUnsavedChangesDialog
71+
=== showUnsavedChangesDialog
5872

5973
The `showUnsavedChangesDialog()` shows a confirmation dialog about discarding unsaved changes. It is used by xref:views/views.adoc#standard-detail-view[entity detail views] when the user closes the view with unsaved changes and xref:ui-properties.adoc#jmix.ui.view.use-save-confirmation[jmix.ui.view.use-save-confirmation] property is set to `false`.
6074

0 commit comments

Comments
 (0)