Can't bind with BeanFieldGroup

[size=2]
[font=tahoma]
I tried to bind a bean with constraints annotation with the field of a FormLayout and use commit to handle validations.

I have:

  • A bean (Person.class) with only property “name” with @NotNull and @UIScoped
  • A FormLayout with a TextField “name” and a Button
  • In my UI a do the bind.
    [/font]
    [/size]

[size=2]
[font=courier new]
@Theme(“mytheme”)
@CDIUI(“”)
@SuppressWarnings(“serial”)
public class MyUI extends UI {
@Inject
private Person person;
private LoginLayout layout;
private BeanFieldGroup binder;

@Override
protected void init(VaadinRequest vaadinRequest) {
    layout = new LoginLayout();
    binder = new BeanFieldGroup<>(Person.class);
    binder.setItemDataSource(person);
    binder.bindMemberFields(layout);
    layout.getButton().addClickListener(e->{
        try{
            binder.commit();
        } catch (Exception ce){
            Notification.show(ce.getMessage(), Notification.Type.ERROR_MESSAGE);
        }
    });
    setContent(layout);
}

}

[font=tahoma]
The commit doesn´t throw any exception.
Even if a initialized the property name = “user” the textfield remains blank. It is like BeanFieldGropu doesn’t bind any bean with any textfield.



Why?


[/font]
[/font]
[/size]

Hi,
try putting bindMemberFields before setItemDataSource, should work.

Regards,
Sergey.

Thanks, Sergey
But it doesn’t work.

It’s strange.
I did it in that order beacuse it is done so in the book-vaadin-examples.
https://github.com/vaadin/book-examples/blob/master/src/com/vaadin/book/examples/datamodel/BeanValidationExample.java#L96

The difference is that in the book, the layout is created and bind the member fields with buildAndBind
and i bind with bindMemberFields using an instance of formLayout.

I tried to validate the empty textField name with three differents manners:

  • adding setRequired to the textField name.
  • adding tha annotation @NotNull in the property name of the bean.
  • addind a validator NotNullValidation in the textField.

The two first work but the last doesn’t.

I use the method commit of the BeanFieldGroup.
I was wonder if commit’s method validate the fields (i think so) but in my case it doesn’t throw CommitException with the validator.

This is my formLayout class.

public class MainLayout extends FormLayout{
    private TextField name;
    private Button valida;

    public MainLayout(){
        name = new TextField("Nombre: ");
        name.setValidationVisible(false);
        name.setNullRepresentation("");
        name.addValidator(new NullValidator("Debes introducir un nombre", true));
        addComponents(name, valida);
    }
...

The listener of the button valida is in a View

... layout.getValida().addClickListener(e-> { try{ layout.visible(); //set the validation visible to true binder.commit(); //binder is the instance of BeanFieldGroup } catch(CommitException ce){ Notification.show(ce.getMessage(), Notification.Type.WARNING_MESSAGE); } });
Why commit doesn’t throw the Exception when the TextField name has a NullValidator?

Regards,
Jose A.

upss

I Found in the API the response.
i was misunderstood the constructor of NullValidator.

NullValidatorpublic NullValidator(java.lang.String errorMessage, boolean onlyNullAllowed)Creates a new NullValidator.

Parameters:
errorMessage - the error message to display on invalidation.
onlyNullAllowed - Are only nulls allowed?

It must be

name.addValidator(new NullValidator("Debes introducir un nombre", false)); It’s a bit confused NullValidator to validate not null fields!!! if you put the second param true.