Grid editor BeanValidationBinder

Hi, Is it possible to use a BeanValidationBinder with Grid editor? I tried without any success.

Hi,

do you have an example of how you tried to do it?

-Olli

@Entity
@Table(name = "User")
public class User extends AbstractTimestampEntity {

    private static final long serialVersionUID = -8735932805533401960L;

    @NotNull
    @Size(min = 1)
    @Column(name = "username", unique = true, nullable = false)
    private String username;

    @NotNull
    @Pattern(regexp = "[a-z0-9._%+-]
+@[a-z0-9.-]
+\\.[a-z]
{2,3}$", message = "Invalid email")
    @Column(name = "email", nullable = false)
    private String email;

    //...getters, setters, constructor
}

[code]
public class UserGrid extends Grid {

private static final long serialVersionUID = -6906747725928178053L;

public UserGrid() {
    super();
    
    setWidth(100, Unit.PERCENTAGE);
    setSelectionMode(SelectionMode.SINGLE);

    BeanValidationBinder<User> binder = new BeanValidationBinder<User>(User.class);
    getEditor().setBinder(binder);

    getEditor().setEnabled(true);

    addColumn(User::getUsername).setId("username");
    addColumn(User::getEmail).setId("email").setEditorComponent(new TextField(), User::setEmail);
}

}
[/code]The validation message is not triggered when I click the Save button in the editor. However, I see that the bean failed to save when I commit the change to my database.

Adding additional validator on the email text field works though. But I don’t want to code the validation twice.

How to use bean validation with grid editor?

thx

You can change this line:
addColumn(User::getEmail).setId(“email”).setEditorComponent(new TextField(), User::setEmail);
to:
addColumn(“email”).setId(“email”).setEditorComponent(new TextField());

It should work. (BeanValidationBinder on ValueProvider does not work)

Thx, it works with the following :

public UserGrid() {
        super(User.class);

        removeAllColumns();

        Binder<User> binder = new BeanValidationBinder<>(User.class);

        getEditor().setEnabled(true);
        getEditor().setBinder(binder);

        addColumn(User::getUsername).setCaption("Utilisateur").setId("username");
        addColumn("email").setEditorComponent(new TextField());
}