use of com.vaadin.flow.data.binder.BinderValidationStatus in project flow-components by vaadin.
the class MainView method createRichTextEditorWithBinder.
private void createRichTextEditorWithBinder() {
RichTextEditor rteWithBinder = new RichTextEditor();
Div valuePanel = new Div();
valuePanel.setId("binder-value-panel");
Div infoPanel = new Div();
Binder<Entry> binder = new Binder<>();
// The object that will be edited
Entry entryBeingEdited = new Entry();
rteWithBinder.setValueChangeMode(ValueChangeMode.EAGER);
// Create the action buttons
Button save = new Button("Save");
Button reset = new Button("Reset");
Button getValueButton = new Button("Get value");
getValueButton.setId("get-binder-rte-value");
getValueButton.addClickListener(event -> {
String value = rteWithBinder.getValue();
valuePanel.setText(value);
});
// Button bar
HorizontalLayout actions = new HorizontalLayout();
actions.add(save, reset, getValueButton);
save.getStyle().set("marginRight", "10px");
SerializablePredicate<String> deltaValuePredicate = value -> !rteWithBinder.getValue().trim().isEmpty();
Binding<Entry, String> deltaValueBinding = binder.forField(rteWithBinder).withValidator(deltaValuePredicate, "Delta value should contain something").bind(Entry::getDeltaValue, Entry::setDeltaValue);
// Editor is a required field
rteWithBinder.setRequiredIndicatorVisible(true);
// Click listeners for the buttons
save.addClickListener(event -> {
if (binder.writeBeanIfValid(entryBeingEdited)) {
infoPanel.setText("Saved bean values: " + entryBeingEdited);
} else {
BinderValidationStatus<Entry> validate = binder.validate();
String errorText = validate.getFieldValidationStatuses().stream().filter(BindingValidationStatus::isError).map(BindingValidationStatus::getMessage).map(Optional::get).distinct().collect(Collectors.joining(", "));
infoPanel.setText("There are errors: " + errorText);
}
});
reset.addClickListener(event -> {
// clear fields by setting null
binder.readBean(null);
infoPanel.setText("");
});
infoPanel.setId("binder-info");
rteWithBinder.setId("binder-delta-value");
save.setId("binder-save");
reset.setId("binder-reset");
add(rteWithBinder, actions, infoPanel, valuePanel);
}
use of com.vaadin.flow.data.binder.BinderValidationStatus in project flow-components by vaadin.
the class FormLayoutView method createFormLayoutWithBinder.
private void createFormLayoutWithBinder() {
FormLayout layoutWithBinder = new FormLayout();
Binder<Contact> binder = new Binder<>();
// The object that will be edited
Contact contactBeingEdited = new Contact();
// Create the fields
TextField firstName = new TextField();
firstName.setValueChangeMode(ValueChangeMode.EAGER);
TextField lastName = new TextField();
lastName.setValueChangeMode(ValueChangeMode.EAGER);
TextField phone = new TextField();
phone.setValueChangeMode(ValueChangeMode.EAGER);
TextField email = new TextField();
email.setValueChangeMode(ValueChangeMode.EAGER);
DatePicker birthDate = new DatePicker();
Checkbox doNotCall = new Checkbox("Do not call");
Label infoLabel = new Label();
NativeButton save = new NativeButton("Save");
NativeButton reset = new NativeButton("Reset");
layoutWithBinder.addFormItem(firstName, "First name");
layoutWithBinder.addFormItem(lastName, "Last name");
layoutWithBinder.addFormItem(birthDate, "Birthdate");
layoutWithBinder.addFormItem(email, "E-mail");
FormItem phoneItem = layoutWithBinder.addFormItem(phone, "Phone");
phoneItem.add(doNotCall);
// Button bar
HorizontalLayout actions = new HorizontalLayout();
actions.add(save, reset);
save.getStyle().set("marginRight", "10px");
SerializablePredicate<String> phoneOrEmailPredicate = value -> !phone.getValue().trim().isEmpty() || !email.getValue().trim().isEmpty();
// E-mail and phone have specific validators
Binding<Contact, String> emailBinding = binder.forField(email).withValidator(phoneOrEmailPredicate, "Both phone and email cannot be empty").withValidator(new EmailValidator("Incorrect email address")).bind(Contact::getEmail, Contact::setEmail);
Binding<Contact, String> phoneBinding = binder.forField(phone).withValidator(phoneOrEmailPredicate, "Both phone and email cannot be empty").bind(Contact::getPhone, Contact::setPhone);
// Trigger cross-field validation when the other field is changed
email.addValueChangeListener(event -> phoneBinding.validate());
phone.addValueChangeListener(event -> emailBinding.validate());
// First name and last name are required fields
firstName.setRequiredIndicatorVisible(true);
lastName.setRequiredIndicatorVisible(true);
binder.forField(firstName).withValidator(new StringLengthValidator("Please add the first name", 1, null)).bind(Contact::getFirstName, Contact::setFirstName);
binder.forField(lastName).withValidator(new StringLengthValidator("Please add the last name", 1, null)).bind(Contact::getLastName, Contact::setLastName);
// Birthdate and doNotCall don't need any special validators
binder.bind(doNotCall, Contact::isDoNotCall, Contact::setDoNotCall);
binder.bind(birthDate, Contact::getBirthDate, Contact::setBirthDate);
// Click listeners for the buttons
save.addClickListener(event -> {
if (binder.writeBeanIfValid(contactBeingEdited)) {
infoLabel.setText("Saved bean values: " + contactBeingEdited);
} else {
BinderValidationStatus<Contact> validate = binder.validate();
String errorText = validate.getFieldValidationStatuses().stream().filter(BindingValidationStatus::isError).map(BindingValidationStatus::getMessage).map(Optional::get).distinct().collect(Collectors.joining(", "));
infoLabel.setText("There are errors: " + errorText);
}
});
reset.addClickListener(event -> {
// clear fields by setting null
binder.readBean(null);
infoLabel.setText("");
doNotCall.setValue(false);
});
infoLabel.setId("binder-info");
firstName.setId("binder-first-name");
lastName.setId("binder-last-name");
phone.setId("binder-phone");
email.setId("binder-email");
birthDate.setId("binder-birth-date");
doNotCall.setId("binder-do-not-call");
save.setId("binder-save");
reset.setId("binder-reset");
addCard("A form layout with fields using Binder", layoutWithBinder, actions, infoLabel);
}
use of com.vaadin.flow.data.binder.BinderValidationStatus in project flow-components by vaadin.
the class MainView method createRichTextEditorWithHtmlBinder.
private void createRichTextEditorWithHtmlBinder() {
RichTextEditor rte = new RichTextEditor();
rte.setId("html-rte");
add(rte);
Div valuePanel = new Div();
valuePanel.setId("html-binder-value-panel");
Div infoPanel = new Div();
Binder<HtmlEntry> binder = new Binder<>();
// The object that will be edited
HtmlEntry entryBeingEdited = new HtmlEntry();
// Create the action buttons
Button save = new Button("Save");
Button reset = new Button("Reset");
Button setBeanHtmlValue = new Button("Set bean html value");
Button getValueButton = new Button("Get value");
getValueButton.setId("get-html-binder-rte-value");
getValueButton.addClickListener(event -> {
String value = rte.asHtml().getValue();
String webcomponentValue = rte.getElement().getProperty("htmlValue");
valuePanel.setText(value + ' ' + webcomponentValue);
});
// Button bar
HorizontalLayout actions = new HorizontalLayout();
actions.add(save, reset, getValueButton, setBeanHtmlValue);
save.getStyle().set("marginRight", "10px");
SerializablePredicate<String> htmlValuePredicate = value -> {
String htmlValue = rte.asHtml().getValue();
return htmlValue != null && !htmlValue.trim().isEmpty();
};
Binding<HtmlEntry, String> asHtmlValueBinding = binder.forField(rte.asHtml()).withValidator(htmlValuePredicate, "html value should contain something").bind(HtmlEntry::getHtmlValue, HtmlEntry::setHtmlValue);
// Editor is a required field
rte.asHtml().setRequiredIndicatorVisible(true);
// Click listeners for the buttons
save.addClickListener(event -> {
if (binder.writeBeanIfValid(entryBeingEdited)) {
infoPanel.setText("Saved bean values: " + entryBeingEdited);
} else {
BinderValidationStatus<HtmlEntry> validate = binder.validate();
String errorText = validate.getFieldValidationStatuses().stream().filter(BindingValidationStatus::isError).map(BindingValidationStatus::getMessage).map(Optional::get).distinct().collect(Collectors.joining(", "));
infoPanel.setText("There are errors: " + errorText);
}
});
reset.addClickListener(event -> {
// clear fields by setting null
binder.readBean(null);
infoPanel.setText("");
});
setBeanHtmlValue.addClickListener(event -> {
entryBeingEdited.setHtmlValue("<p><b>Foo</b></p>");
binder.readBean(entryBeingEdited);
});
infoPanel.setId("html-binder-info");
save.setId("html-binder-save");
setBeanHtmlValue.setId("html-binder-set-bean-value");
reset.setId("html-binder-reset");
add(actions, infoPanel, valuePanel);
}
Aggregations