Search code examples
javavaadinvaadin7

How to only show errors on commit of BeanFieldGroup?


I have a BeanFieldGroup with several validators, and a Save button that commits the form. But if I have eg a @NotBlank validation constraint on my entity, and the input field is empty by default, I already get an error icon beneath initially when showing the form.

  • How can I prevent this?
  • How can I then show the icons again if there are any validation errors on commit?
  • Check if the BeanFieldGroup has any validation errors?
public class UserView {
    private BeanFieldGroup<User> editor;
    private Button saveBtn;

    public UserView() {
        saveBtn = new Button("Save", new ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                try {
                    editor.commit();
                } catch (CommitException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

Solution

  • For checking for errors there is isValid https://vaadin.com/api/7.2.0/com/vaadin/data/fieldgroup/FieldGroup.html#isValid%28%29 (see the implementation: it iterates over the fields and calls validate more or less). if you need the errors you can pick them up there. after the commit you might also use the javax.validation machinery again on the object itself.

    for hiding the errors you could play around with AbstractFields' setValidationVisible method.