From 28f5c845793be9a0db3f2acef914a68ccc91ce80 Mon Sep 17 00:00:00 2001
From: Pekka Hyvönen <pekka@vaadin.com>
Date: Mon, 12 Sep 2016 12:05:19 +0300
Subject: Refactor Binder Status Handling API

BinderStatusHandler is now triggered only once per validation.
Unified ValidationError and BinderResult into BinderValidationStatus.
Renamed ValidationStatusChangeEvent into ValidationStatus.
Unified handler names for validation status.

Next patch will fix resetting of field errors on reset.

Change-Id: I9536d554d781fe599fbd7e5bcb5a9ffebe675ca0
---
 documentation/datamodel/datamodel-forms.asciidoc | 34 +++++++++++-------------
 1 file changed, 15 insertions(+), 19 deletions(-)

(limited to 'documentation/datamodel/datamodel-forms.asciidoc')

diff --git a/documentation/datamodel/datamodel-forms.asciidoc b/documentation/datamodel/datamodel-forms.asciidoc
index 62fd9df38c..62d844dee6 100644
--- a/documentation/datamodel/datamodel-forms.asciidoc
+++ b/documentation/datamodel/datamodel-forms.asciidoc
@@ -323,11 +323,11 @@ Even if the user has not edited a field, all validation error will be shown if w
 binder.load(new Person());
 
 // This will make all current validation errors visible
-List<ValidationError<?>> validationErrors = binder.validate();
+ValidationStatus<Person> status = binder.validate();
 
-if (!validationErrors.isEmpty()) {
+if (status.hasErrors()) {
   Notification.show("Validation error count: "
-    + validationErrors.size());
+    + status.getValidationErrors().size());
 }
 ----
 
@@ -341,7 +341,7 @@ Handling a checked exception::
 ----
 try {
   binder.save(person);
-} catch (BindingException e) {
+} catch (ValidationException e) {
   Notification.show("Validation error count: "
     + e.getValidationErrors().size());
 }
@@ -566,21 +566,17 @@ We can also define our own status handler to provide a custom way of handling st
 ----
 BinderStatusHandler defaultHandler = binder.getStatusHandler();
 
-binder.setStatusHandler(results -> {
-  String errorMessage = results.stream()
-    // Ignore helper and confirmation messages
-    .filter(BinderResult::isError)
-    // Ignore messages that belong to a specific field
-    .filter(error -> !error.getField().isPresent())
-    // Create a string out of the remaining messages
-    .map(Result::getMessage).map(o -> o.get())
-    .collect(Collectors.joining("\n"));
-
-  formStatusLabel.setValue(errorMessage);
-  formStatusLabel.setVisible(!errorMessage.isEmpty());
-
-  // Let the default handler show messages for each field
-  defaultHandler.accept(event);
+binder.setStatusHandler(status -> {
+    // create an error message on failed bean level validations
+    List<Result<?>> errors = status.getBeanValidationErrors();
+    String errorMessage = errors.stream().map(Result::getMessage)
+        .map(o -> o.get()).collect(Collectors.joining("\n"));
+    // show error in a label
+    formStatusLabel.setValue(errorMessage);
+    formStatusLabel.setVisible(!errorMessage.isEmpty());
+
+    // Let the default handler show messages for each field
+    defaultHandler.accept(status);
 });
 ----
 
-- 
cgit v1.2.3

