You can create an Exception Handler for the whole UI part as described in the documentation
Something like this.
Entity listener with validation in core module:
@Component("sessionplanner_SessionListener")
public class SessionEntityListener implements BeforeInsertEntityListener<Session> {
@Override
public void onBeforeInsert(Session entity, EntityManager entityManager) {
if (entity.getStartDate().isBefore(LocalDateTime.now())) {
throw new CustomValidationException("Cannot plan session!");
}
}
}
Exception handler in web module:
@Component(ValidationExeptionHandler.NAME)
public class ValidationExeptionHandler extends AbstractUiExceptionHandler {
public static final String NAME = "sessionplanner_ValidationExeptionHandler";
public ValidationExeptionHandler() {
super(CustomValidationException.class.getName());
}
@Override
protected void doHandle(String className, String message, @Nullable Throwable throwable, UiContext context) {
context.getNotifications().create(Notifications.NotificationType.ERROR)
.withCaption("Error")
.withDescription(message)
.show();
}
}