2

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();
                }
            }
        });
    }
}
Draken
  • 3,134
  • 13
  • 34
  • 54
membersound
  • 81,582
  • 193
  • 585
  • 1,120

1 Answers1

1

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.

cfrick
  • 35,203
  • 6
  • 56
  • 68
  • Hm ok, but as far as I can see `isValid()` calls the validation again. And, curing `editor.commit()`, the validation is called another time. I'd prefer to only have the validation called once. But that's not possible? – membersound May 20 '14 at 09:19
  • `setValidationVisible` works, thanks! Though I'd prefer to have a method call on the beangroup to just show and hide the validation. But ok, I could extend the beangroup myself for this purpose as well. – membersound May 20 '14 at 09:21
  • subclass the binder and add it there. the beangroup knows its fields. also you can just commit and then see, if there are errors (e.g. find fields of the binder with componentError) – cfrick May 20 '14 at 09:35