Skip to content

Commit

Permalink
Remove duplicate error message (#631)
Browse files Browse the repository at this point in the history
  • Loading branch information
KochTobi authored Jun 18, 2024
1 parent ec2fca1 commit 6af5380
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import com.vaadin.flow.data.binder.ValidationResult;

/**
* Mixin interface for components that provide a binder for validation.
* Sets properties for invalid state and error message string to show when invalid.
* Mixin interface for components that provide a binder for validation. Sets properties for invalid
* state and error message string to show when invalid.
*/
public interface HasBinderValidation<T> extends HasValidationProperties {

Expand Down Expand Up @@ -48,15 +48,30 @@ default HasBinderValidation<T> validate() {
requireNonNull(getBinder(), "getBinder() must not be null");
BinderValidationStatus<T> validationStatus = getBinder().validate();
setInvalid(validationStatus.hasErrors());
setErrorMessage(validationStatus
.getValidationErrors().stream()
.filter(it -> !it.getErrorMessage().isBlank())
.findFirst()
.map(ValidationResult::getErrorMessage)
.orElse(getDefaultErrorMessage()));
if (useBinderErrorMessage()) {
setErrorMessage(validationStatus
.getValidationErrors().stream()
.filter(it -> !it.getErrorMessage().isBlank())
.findFirst()
.map(ValidationResult::getErrorMessage)
.orElse(getDefaultErrorMessage()));
} else {
setErrorMessage(getDefaultErrorMessage());
}
return this;
}

/**
* True if the binder error message should be used. If false, then the default error message is
* used always.
*
* @return true if the binder error message is used; false if the default error message is used
* only.
*/
default boolean useBinderErrorMessage() {
return true;
}

/**
* Missing method from HasValidationProperties. Indiates whether this is marked as invalid.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,16 @@ public Contact getEmptyValue() {
return new Contact(nameField.getEmptyValue(), emailField.getEmptyValue());
}

@Override
public String getDefaultErrorMessage() {
return ""; // we do not show an error message on this field as this is part of the contained fields.
}

@Override
public boolean useBinderErrorMessage() {
return false;
}

@Override
public Binder<Contact> getBinder() {
return binder;
Expand Down

0 comments on commit 6af5380

Please sign in to comment.