How to get all bean validation error messages from BeanValidationBinder or

For example:

  • Domain Model
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.NotEmpty;

import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

@Data
@NoArgsConstructor
public class UserId {
    @NotEmpty(message = "'User Id' is a mandatory field.")
    @Pattern(regexp = "^[a-zA-Z0-9]
+$", message = "Valid value of 'User id' is half-width alphanumeric.")
    @Size(min = 1, max = 30, message = "Max size of 'User id' is {max}.")
    String value;

    public UserId(String value) {
        this.value = value;
    }

    public String asText() {
        return value;
    }
}
  • TextField corresponding to the domain model
import com.example.demo.domain.model.user.UserId;
import com.vaadin.data.BeanValidationBinder;
import com.vaadin.ui.TextField;

class UserIdInputField extends TextField {
final BeanValidationBinder<UserId> binder = new BeanValidationBinder<>(UserId.class);

    UserIdInputField() {
        binder.bind(this, "value");
    }
}
  • I tried to get all validation error messages as below code.
// my form field
final UserIdInputField userId = new UserIdInputField();

...

// for example, after "save" button clicked
BinderValidationStatus<UserId> status = userId.binder.validate();

// calling below methods, I expected return 3 result, but returned 1 result,
// besides validation order seems to be random.
List<ValidationResult> validationErrors = status.getValidationErrors();
List<BindingValidationStatus<?>> fieldValidationErrors = status.getFieldValidationErrors();
List<BindingValidationStatus<?>> fieldValidationStatuses = status.getFieldValidationStatuses();

// calling below methods, returned no result.
List<ValidationResult> beanValidationErrors = status.getBeanValidationErrors();
List<ValidationResult> beanValidationResults = status.getBeanValidationResults();

// of course, could only get 1 error message from component itself.
ErrorMessage errorMessage = userId.getErrorMessage();
ErrorMessage errorMessage = userId.getComponentError();

I’d like some advice.

This class https://github.com/vaadin/framework/blob/master/server/src/main/java/com/vaadin/data/validator/BeanValidator.java only retrieve the 1st error message (line 125-128).
So if you want all the errors linked to the attribute you can rewrite BeanValidationBinder (and change the BeanValidator).

EasyBinder add-on may help you (I didn’t try it). Because there are some missing functionalities inside BeanValidationBinder :/.