Last active
March 29, 2018 15:03
-
-
Save alejandro-du/576474c5803205032f89af4e75c50f72 to your computer and use it in GitHub Desktop.
Getting WARNING level validation results with Binder in Vaadin 8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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