Data Grid Validation

I am trying to add some custom validation to my Data Grid which has inline editing enabled. I have added my validation to the PreCommitListener of the Data Grid but there does not seem to be a way to stop the commit from within this listener. In other elements you can return a Boolean value of false to stop the commit from occurring, this is not possible with the Data Grid editor as you can see below. Is there another standard approach I should be using?

/**
 * DataGrid editor pre commit listener. Works in buffered mode only.
 */
interface EditorPreCommitListener {
    /**
     * Called before the item is updated.
     *
     * @param event an event providing more information
     */
    void preCommit(EditorPreCommitEvent event);
}

Hi,

For now, you can either throw a new com.vaadin.data.Validator.InvalidValueException or add a custom validator to editing fields, for example:

customersDataGrid.getColumnNN("name").setEditorFieldGenerator((datasource, property) -> {
    TextField textField = componentsFactory.createComponent(TextField.class);
    textField.setDatasource(datasource, property);
    textField.addValidator(value -> {
        throw new ValidationException("Test");
    });
    return textField;
});

customersDataGrid.addEditorPreCommitListener(event -> {
    throw new Validator.InvalidValueException("Test");
});

Regards,
Gleb

1 Like

Thank you I ended up adding the custom validator to the field which works great.

I do want to point out that I did attempt to throw the Invalid Value Exception, which did successfully stop the commit and show an error however I could not seem to access the values that had been entered into the datagrid editor, I could only access the edited item’s Id. This would allow me to select the item from the data source however because this is occurring in the pre-commit method the newly entered values that exist in the datagrid editor are not in the data source yet. This prevented me from performing any form of useful validation.

Corey Amoruso