Skip to content

Instantly share code, notes, and snippets.

@alejandro-du
Last active March 29, 2018 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alejandro-du/576474c5803205032f89af4e75c50f72 to your computer and use it in GitHub Desktop.
Save alejandro-du/576474c5803205032f89af4e75c50f72 to your computer and use it in GitHub Desktop.
Getting WARNING level validation results with Binder in Vaadin 8
package com.vaadin.example;
import com.vaadin.data.Binder;
import com.vaadin.data.ValidationResult;
import com.vaadin.server.VaadinRequest;
import com.vaadin.shared.ui.ErrorLevel;
import com.vaadin.ui.Notification;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
/**
* @author Alejandro Duarte.
*/
public class VaadinUI extends UI {
public static class Bean {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
@Override
protected void init(VaadinRequest vaadinRequest) {
TextField textField = new TextField();
Binder<Bean> binder = new Binder<>();
binder.forField(textField)
.withValidator((v, c) -> ValidationResult.create("This is a warning!", ErrorLevel.WARNING))
.withValidator((v, c) -> ValidationResult.error("This is an error!"))
.bind(Bean::getValue, Bean::setValue);
binder.setBean(new Bean());
binder.setValidationStatusHandler(statusChange ->
statusChange.getFieldValidationStatuses().stream()
.map(r -> r.getValidationResults())
.flatMap(l -> l.stream())
.filter(r -> ErrorLevel.WARNING.equals(r.getErrorLevel().orElse(null)))
.forEach(w -> Notification.show(w.getErrorMessage()))
);
binder.validate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment