Skip to content

Commit

Permalink
Refactor Binder Status Handling API
Browse files Browse the repository at this point in the history
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
  • Loading branch information
pleku authored and Vaadin Code Review committed Sep 12, 2016
1 parent 363dce9 commit 28f5c84
Show file tree
Hide file tree
Showing 15 changed files with 886 additions and 640 deletions.
34 changes: 15 additions & 19 deletions documentation/datamodel/datamodel-forms.asciidoc
Expand Up @@ -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()); binder.load(new Person());
// This will make all current validation errors visible // 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: " Notification.show("Validation error count: "
+ validationErrors.size()); + status.getValidationErrors().size());
} }
---- ----


Expand All @@ -341,7 +341,7 @@ Handling a checked exception::
---- ----
try { try {
binder.save(person); binder.save(person);
} catch (BindingException e) { } catch (ValidationException e) {
Notification.show("Validation error count: " Notification.show("Validation error count: "
+ e.getValidationErrors().size()); + e.getValidationErrors().size());
} }
Expand Down Expand Up @@ -566,21 +566,17 @@ We can also define our own status handler to provide a custom way of handling st
---- ----
BinderStatusHandler defaultHandler = binder.getStatusHandler(); BinderStatusHandler defaultHandler = binder.getStatusHandler();
binder.setStatusHandler(results -> { binder.setStatusHandler(status -> {
String errorMessage = results.stream() // create an error message on failed bean level validations
// Ignore helper and confirmation messages List<Result<?>> errors = status.getBeanValidationErrors();
.filter(BinderResult::isError) String errorMessage = errors.stream().map(Result::getMessage)
// Ignore messages that belong to a specific field .map(o -> o.get()).collect(Collectors.joining("\n"));
.filter(error -> !error.getField().isPresent()) // show error in a label
// Create a string out of the remaining messages formStatusLabel.setValue(errorMessage);
.map(Result::getMessage).map(o -> o.get()) formStatusLabel.setVisible(!errorMessage.isEmpty());
.collect(Collectors.joining("\n"));
// Let the default handler show messages for each field
formStatusLabel.setValue(errorMessage); defaultHandler.accept(status);
formStatusLabel.setVisible(!errorMessage.isEmpty());
// Let the default handler show messages for each field
defaultHandler.accept(event);
}); });
---- ----


Expand Down
4 changes: 2 additions & 2 deletions server/src/main/java/com/vaadin/data/BeanBinder.java
Expand Up @@ -149,7 +149,7 @@ protected static class BeanBindingImpl<BEAN, FIELDVALUE, TARGET>
protected BeanBindingImpl(BeanBinder<BEAN> binder, protected BeanBindingImpl(BeanBinder<BEAN> binder,
HasValue<FIELDVALUE> field, HasValue<FIELDVALUE> field,
Converter<FIELDVALUE, TARGET> converter, Converter<FIELDVALUE, TARGET> converter,
StatusChangeHandler statusChangeHandler) { ValidationStatusHandler statusChangeHandler) {
super(binder, field, converter, statusChangeHandler); super(binder, field, converter, statusChangeHandler);
} }


Expand Down Expand Up @@ -318,7 +318,7 @@ public BeanBinder<BEAN> withValidator(Validator<? super BEAN> validator) {
@Override @Override
protected <FIELDVALUE, TARGET> BeanBindingImpl<BEAN, FIELDVALUE, TARGET> createBinding( protected <FIELDVALUE, TARGET> BeanBindingImpl<BEAN, FIELDVALUE, TARGET> createBinding(
HasValue<FIELDVALUE> field, Converter<FIELDVALUE, TARGET> converter, HasValue<FIELDVALUE> field, Converter<FIELDVALUE, TARGET> converter,
StatusChangeHandler handler) { ValidationStatusHandler handler) {
Objects.requireNonNull(field, "field cannot be null"); Objects.requireNonNull(field, "field cannot be null");
Objects.requireNonNull(converter, "converter cannot be null"); Objects.requireNonNull(converter, "converter cannot be null");
return new BeanBindingImpl<>(this, field, converter, handler); return new BeanBindingImpl<>(this, field, converter, handler);
Expand Down

0 comments on commit 28f5c84

Please sign in to comment.