Table validation in postValidate

Hi, I also came across this problem, could you tell me how do I validate the column in postValidate method?

Hello, @zuco_ivan

First of all, to perform table validation you should add a validator:

private final static Pattern PHONE_REGEX = Pattern.compile("[0-9]+");
private final static String EXC_MSG = "Phone should contain only digits. (Entity: %s %s %s)";

@Inject
private GroupTable<Employee> employeesTable;

@Override
public void init(Map<String, Object> params) {
    super.init(params);

    employeesTable.addValidator(value -> {
        if (value == null)
            return;

        //noinspection unchecked
        for (Employee employee : (Collection<Employee>) value) {
            if (!PHONE_REGEX.matcher(employee.getPhone()).matches()) {
                throw new ValidationException(String.format(EXC_MSG,
                        employee.getFirstName(), employee.getLastName(), employee.getPhone()));
            }
        }
    });
}

Then you invoke table validation into the postValidate() method and in case of error add a reason:

public void saveAll() {
    if (validateAll()) {
        showNotification("Saved!", NotificationType.TRAY);
    }
}

@Override
protected void postValidate(ValidationErrors errors) {
    super.postValidate(errors);

    try {
        ((WebGroupTable<Employee>) employeesTable).validate();
    } catch (ValidationException e) {
        errors.add(employeesTable, e.getMessage());
    }
}

Result:
validation

Also I would recommend you to check master-detail screen that can be generated via CUBA Studio:

image

Regards,
Daniil

3 Likes