Validate LibreOffice Base Forms Using Macro Events
Automating data validation in LibreOffice Base ensures that entered data adheres to specific constraints before it is written to the underlying database. By binding Basic or Python macros to specific form and control events, you can intercept user actions, validate values against business rules, and cancel write operations if the validation fails. This guide covers the essential event bindings at both the control and form levels required to build an automated validation workflow in LibreOffice Base.
Control-Level Validation Events
Control-level events trigger based on user interactions with specific input fields (such as text boxes, numeric fields, or combo boxes). These events are ideal for single-field syntax and range checks.
- Before Updating
(
com.sun.star.form.XUpdateListener): This is the primary event for field-specific validation. It executes immediately after the user finishes editing a field but before the new value is committed to the data model. If the macro determines that the input is invalid, it can reject the update, preventing bad data from entering the buffer. - Loss of Focus
(
com.sun.star.awt.XFocusListener): Triggers when the cursor leaves a control. This event is best used for visual validation feedback, such as changing the background color of an invalid field or displaying an immediate tool-tip warning, without immediately interfering with the database write buffer. - Text Modified
(
com.sun.star.awt.XTextListener): Fires with every keystroke or change in a text control. Use this event for real-time masking, character-limit enforcement, or dynamic formatting (such as auto-formatting phone numbers or capitalization).
Form-Level Validation Events
Form-level events operate on the record as a whole, making them necessary for multi-field cross-validation (e.g., ensuring an end date occurs after a start date) and final database integrity checks.
- Before Record Action / Prior to Record Action
(
com.sun.star.form.XRowSetApproveListener): This event fires immediately before a record insert, update, or delete operation is executed against the database. It is the most critical event for comprehensive record validation. ReturningFalsein your bound macro cancels the entire save or delete action, keeping the user on the form until the errors are resolved. - Before Submitting: Applies to forms designed to submit data to external targets or web endpoints. It allows validation of all form fields collectively before transmission.
- Confirm Delete
(
com.sun.star.form.XConfirmDeleteListener): Triggers when a user attempts to delete a record. While primarily used for custom confirmation dialogues, it can also validate whether a record is eligible for deletion according to relational rules.
Implementing Event-Based Cancellation
To intercept and abort an operation during validation, configure your macro to bind to the Prior to record action or Before updating events:
- Open the form in Edit Mode.
- Right-click the target control (for field validation) or the form container via the Form Navigator (for record validation) and select Properties.
- Navigate to the Events tab.
- Locate the desired event (e.g., Prior to record action or
Before updating) and click the ellipsis (
...) to assign your macro. - In your macro code, evaluate the field values. To cancel the action,
pass
Falseback to the event handler or invoke the event object’s veto method, which stops the write process and keeps the form active for corrections.