Search in sources :

Example 1 with BindingValidationStatus

use of com.vaadin.flow.data.binder.BindingValidationStatus 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);
}
Also used : BinderValidationStatus(com.vaadin.flow.data.binder.BinderValidationStatus) Binder(com.vaadin.flow.data.binder.Binder) HorizontalLayout(com.vaadin.flow.component.orderedlayout.HorizontalLayout) VerticalLayout(com.vaadin.flow.component.orderedlayout.VerticalLayout) Div(com.vaadin.flow.component.html.Div) Collectors(java.util.stream.Collectors) Route(com.vaadin.flow.router.Route) Serializable(java.io.Serializable) Button(com.vaadin.flow.component.button.Button) RichTextEditor(com.vaadin.flow.component.richtexteditor.RichTextEditor) BindingValidationStatus(com.vaadin.flow.data.binder.BindingValidationStatus) Optional(java.util.Optional) SerializablePredicate(com.vaadin.flow.function.SerializablePredicate) Binding(com.vaadin.flow.data.binder.Binder.Binding) ValueChangeMode(com.vaadin.flow.data.value.ValueChangeMode) Optional(java.util.Optional) BindingValidationStatus(com.vaadin.flow.data.binder.BindingValidationStatus) HorizontalLayout(com.vaadin.flow.component.orderedlayout.HorizontalLayout) Div(com.vaadin.flow.component.html.Div) Binder(com.vaadin.flow.data.binder.Binder) Button(com.vaadin.flow.component.button.Button) RichTextEditor(com.vaadin.flow.component.richtexteditor.RichTextEditor)

Example 2 with BindingValidationStatus

use of com.vaadin.flow.data.binder.BindingValidationStatus 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);
}
Also used : FormLayout(com.vaadin.flow.component.formlayout.FormLayout) BinderValidationStatus(com.vaadin.flow.data.binder.BinderValidationStatus) Component(com.vaadin.flow.component.Component) Composite(com.vaadin.flow.component.Composite) EmailValidator(com.vaadin.flow.data.validator.EmailValidator) Binder(com.vaadin.flow.data.binder.Binder) HorizontalLayout(com.vaadin.flow.component.orderedlayout.HorizontalLayout) Div(com.vaadin.flow.component.html.Div) Label(com.vaadin.flow.component.html.Label) NativeButton(com.vaadin.flow.component.html.NativeButton) ResponsiveStep(com.vaadin.flow.component.formlayout.FormLayout.ResponsiveStep) FormItem(com.vaadin.flow.component.formlayout.FormLayout.FormItem) Route(com.vaadin.flow.router.Route) BindingValidationStatus(com.vaadin.flow.data.binder.BindingValidationStatus) SerializablePredicate(com.vaadin.flow.function.SerializablePredicate) TextField(com.vaadin.flow.component.textfield.TextField) ValueChangeMode(com.vaadin.flow.data.value.ValueChangeMode) VerticalLayout(com.vaadin.flow.component.orderedlayout.VerticalLayout) H2(com.vaadin.flow.component.html.H2) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) FormLayout(com.vaadin.flow.component.formlayout.FormLayout) Checkbox(com.vaadin.flow.component.checkbox.Checkbox) StringLengthValidator(com.vaadin.flow.data.validator.StringLengthValidator) DatePicker(com.vaadin.flow.component.datepicker.DatePicker) LocalDate(java.time.LocalDate) Optional(java.util.Optional) Binding(com.vaadin.flow.data.binder.Binder.Binding) NativeButton(com.vaadin.flow.component.html.NativeButton) EmailValidator(com.vaadin.flow.data.validator.EmailValidator) Optional(java.util.Optional) FormItem(com.vaadin.flow.component.formlayout.FormLayout.FormItem) StringLengthValidator(com.vaadin.flow.data.validator.StringLengthValidator) Label(com.vaadin.flow.component.html.Label) BindingValidationStatus(com.vaadin.flow.data.binder.BindingValidationStatus) HorizontalLayout(com.vaadin.flow.component.orderedlayout.HorizontalLayout) Binder(com.vaadin.flow.data.binder.Binder) Checkbox(com.vaadin.flow.component.checkbox.Checkbox) TextField(com.vaadin.flow.component.textfield.TextField) DatePicker(com.vaadin.flow.component.datepicker.DatePicker)

Example 3 with BindingValidationStatus

use of com.vaadin.flow.data.binder.BindingValidationStatus 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);
}
Also used : BinderValidationStatus(com.vaadin.flow.data.binder.BinderValidationStatus) Binder(com.vaadin.flow.data.binder.Binder) HorizontalLayout(com.vaadin.flow.component.orderedlayout.HorizontalLayout) VerticalLayout(com.vaadin.flow.component.orderedlayout.VerticalLayout) Div(com.vaadin.flow.component.html.Div) Collectors(java.util.stream.Collectors) Route(com.vaadin.flow.router.Route) Serializable(java.io.Serializable) Button(com.vaadin.flow.component.button.Button) RichTextEditor(com.vaadin.flow.component.richtexteditor.RichTextEditor) BindingValidationStatus(com.vaadin.flow.data.binder.BindingValidationStatus) Optional(java.util.Optional) SerializablePredicate(com.vaadin.flow.function.SerializablePredicate) Binding(com.vaadin.flow.data.binder.Binder.Binding) ValueChangeMode(com.vaadin.flow.data.value.ValueChangeMode) Optional(java.util.Optional) BindingValidationStatus(com.vaadin.flow.data.binder.BindingValidationStatus) HorizontalLayout(com.vaadin.flow.component.orderedlayout.HorizontalLayout) Div(com.vaadin.flow.component.html.Div) Binder(com.vaadin.flow.data.binder.Binder) Button(com.vaadin.flow.component.button.Button) RichTextEditor(com.vaadin.flow.component.richtexteditor.RichTextEditor)

Aggregations

Div (com.vaadin.flow.component.html.Div)3 HorizontalLayout (com.vaadin.flow.component.orderedlayout.HorizontalLayout)3 VerticalLayout (com.vaadin.flow.component.orderedlayout.VerticalLayout)3 Binder (com.vaadin.flow.data.binder.Binder)3 Binding (com.vaadin.flow.data.binder.Binder.Binding)3 BinderValidationStatus (com.vaadin.flow.data.binder.BinderValidationStatus)3 BindingValidationStatus (com.vaadin.flow.data.binder.BindingValidationStatus)3 ValueChangeMode (com.vaadin.flow.data.value.ValueChangeMode)3 SerializablePredicate (com.vaadin.flow.function.SerializablePredicate)3 Route (com.vaadin.flow.router.Route)3 Serializable (java.io.Serializable)3 Optional (java.util.Optional)3 Collectors (java.util.stream.Collectors)3 Button (com.vaadin.flow.component.button.Button)2 RichTextEditor (com.vaadin.flow.component.richtexteditor.RichTextEditor)2 Component (com.vaadin.flow.component.Component)1 Composite (com.vaadin.flow.component.Composite)1 Checkbox (com.vaadin.flow.component.checkbox.Checkbox)1 DatePicker (com.vaadin.flow.component.datepicker.DatePicker)1 FormLayout (com.vaadin.flow.component.formlayout.FormLayout)1