Validator for Table columns

Do you have any plan or think it will be a good idea to add column validation option in Studio like we have for textFields?

Hi,
We do not currently have plans to add automatic validation of Table due to technical limitations of fields rendering inside Table. For now, you can only add Validator to Table.Column programmatically but you have to run them manually from your code.

You can run such a table validation in one of the life-cycle methods of AbstractEditor/AbstractWindow, for instance in preCommit or postValidate, see [url=https://doc.cuba-platform.com/manual-6.2/abstractWindow.html]https://doc.cuba-platform.com/manual-6.2/abstractWindow.html[/url] and [url=https://doc.cuba-platform.com/manual-6.2/abstractEditor.html]https://doc.cuba-platform.com/manual-6.2/abstractEditor.html[/url].

Hi Yuriy,

I’m trying to follow your advice here, but currently it is not possible to rely on CUBA validation framework for Table validation.

Even if one uses Table.addValidator(), such a validator will not be called by the framework, because Table interface does not implement Component.Validatable. Verified and tested.

If we look at the following code in WebWindow, we can see that it relies on Component.Validatable interface to call validate() on the component.


   public boolean validateAll() {
        ValidationErrors errors = new ValidationErrors();

        Collection<Component> components = ComponentsHelper.getComponents(this);
        for (Component component : components) {
            if (component instanceof Validatable) {
                try {
                    ((Validatable) component).validate();
                } catch (ValidationException e) {
                    if (log.isTraceEnabled())
                        log.trace("Validation failed", e);
                    else if (log.isDebugEnabled())
                        log.debug("Validation failed: " + e);

                    ComponentsHelper.fillErrorMessages((Validatable) component, e, errors);
                }
            }
        }
        validateAdditionalRules(errors);
        return handleValidationErrors(errors);
    }

Moreover, in WebAbstractTable we can see that the validate() method of the Component.Validatable is implemented (see below), and the job seems correctly done.


    public void validate() throws ValidationException {
        if (tableValidators != null) {
            for (com.haulmont.cuba.gui.components.Field.Validator tableValidator : tableValidators) {
                tableValidator.validate(getSelected());
            }
        }
    }

However the isValid() method, second method to implement in Component.Validatable, is not implemented.

I understand the issue of Component in table lines being limited. But as a first step, we could be allowed to use the BeanValidation on table items. This is what I’m currently trying to do.

Or maybe I’m completely wrong, in that case do not hesitate to correct me.

1 Like

Hi,

As I mentioned before, Table does not trigger its Validator’s. You have to call it yourself from preCommit or postValidate methods of a screen controller. This validators collection is just old API that we haven’t removed yet from the framework.

For now, you can only add Validator to Table.Column programmatically but you have to run them manually from your code.

See also postValidate usage: AbstractWindow - CUBA Platform. Developer’s Manual

Yes, I got that. But in order to run the validators, we have to downcast from Table interface to the underlying implementation. Not safe, and probably not future-proof if you say that this is old API, it will probably disappear at some point.

For now we are trying to build a Table Component allowing inline edit with the same validations that we would have in a form, as explained here.

Calling the validators through downcasting is ok. The most annoying is that to reproduce the Editor form validation logic, we need to instantiate ourselves all BeanValidator and add them manually on each corresponding Table.Column. This is duplicating of CUBA logic where it is done automatically for Fields when building an Editor form, again not elegant and not future-proof.

If we cannot find a satisfying way for inline table edit, we will have to fallback to the entity browser/editor combo.

1 Like

Yes, you are right. We will discuss it and try to provide more accurate API for validation of editable Tables.

Also, I recommend that you take a look at the new DataGrid editor feature: DataGrid - CUBA Platform. Developer’s Manual DataGrid triggers validation for editor cells automatically.

2 posts were split to a new topic: Table validation in postValidate