Data Validation

From Apache OpenOffice Wiki
Jump to: navigation, search



If you happen to have a scripting language that is not capable of creating own components, such as StarBasic, then the validation mechanisms described in chapter Validation can not be used: They rely on a component being created that implements the XValidator interface.

If, despite this, you want to validate data in controls bound to a database, then you have two alternative possibilities:

  • From the chapter Committing Controls, you can approve updates, and veto the changes a control wants to write into the data column it is bound to.
  • Additionally, you can step in later. You know how to use a com.sun.star.sdb.XRowSetApproveListener for doing last-minute changes to a record that is about to be inserted.
Besides this, you can use the listener to approve changes to the row set data. When the com.sun.star.sdb.RowChangeAction is sent to the listeners, it distinguishes between different kinds of data modification. You can implement listeners that act differently for insertions and simple updates.

Note the important differences between both solutions. Using an com.sun.star.form.XUpdateListener implies that the data operations are vetoed for a given control. Your listener is invoked as soon as the respective control is committed, for instance, when it loses the focus. This implies that changes done to the data column by other means than through this control are not monitored.

The second alternative is using an com.sun.star.sdb.XRowSetApproveListener meaning you veto changes immediately before they are sent to the database. Thus, it is irrelevant where they have been made previously. In addition, error messages that are raised when the user actively tries to save the record are considered less disturbing than error messages raised when the user simply leaves a control.

The example below shows the handling for denying empty values for a given control:

  public boolean approveUpdate(EventObject aEvent) throws com.sun.star.uno.RuntimeException {
      boolean bApproved = true;
 
      // the control model which fired the event
      XPropertySet xSourceProps = UNO.queryPropertySet(aEvent.Source);
 
      String sNewText = (String)xSourceProps.getPropertyValue("Text");
      if (0 == sNewText.length()) {
          // say that the value is invalid
          showInvalidValueMessage();
          bApproved = false;
 
          // reset the control value
          // for this, we take the current value from the row set field the control
          // is bound to, and forward it to the control model
          XColumn xBoundColumn = UNO.queryColumn(xSourceProps.getPropertyValue("BoundField"));
          if (null != xBoundColumn) {
              xSourceProps.setPropertyValue("Text", xBoundColumn.getString());
          }
      }
 
      return bApproved;
  }
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages