How to get the binder.validate() errors

Hi,

I defined a form with a lot of fields, and used a binder to bind them. However, when running binder.validate(), I couldn’t get the detailed errors of which fields get wrong. Any ways that I can display to user which fields got what errors?

Best regards,
Joey

Try this…
try {
binder.writeBean(item);
} catch (ValidationException e) {
System.out.println(AbstractCVaadinUIHelper.getValidationExceptionsAsString(e, “\n”));
}

public static String getValidationExceptionsAsString(Exception e, String msgDelimiter) {
    String reasons = "";
    if (msgDelimiter == null || msgDelimiter.isEmpty()) {
        msgDelimiter = ",";
    }
    if (e instanceof ValidationException) {
        ValidationException ve = (ValidationException) e;

        for (ValidationResult validationError : ve.getValidationErrors()) {
            reasons += validationError.getErrorMessage() + msgDelimiter;
        }
    }
    return reasons;
}

Not 100% if I understood your problem correctly, but what about binder.validate().getFieldValidationErrors()? From that list you can iterate through all field related errors and can also show for each error the validation results with error message and level plus the related field.

for (BindingValidationStatus<?> fieldValidationError : binder.validate().getFieldValidationErrors()) {
	HasValue<?, ?> field = fieldValidationError.getField();
	for (ValidationResult validationResult : fieldValidationError.getValidationResults()) {
		String errorMessage = validationResult.getErrorMessage();
		// ...
	}
}

PS: With the Binder’s status change listener or value change listener you can also check things directly when the Binder or bound fields have changed.

Thanks a lot.