Add Field Validation to a Form

Field validation is often added to forms to enforce consistency of data across the application or as input criteria for other data processing within the database. This can be accomplished by adding data checks in the "Input Validation" event for each individual field or alternatively in the QuerySave event.

Although either approach will produce the same result, you will most likely find that the QuerySave event option provides a consolidated location for all field validation. Using this approach, all data checks are in a common location, which allows all invalid data fields to be displayed in a single message.

A.13.1

 

How It Works

Form validation occurs when the user attempts to save the document. When this occurs, the validation in the QuerySave event is triggered. A text message field (MsgText) is used to track all warning messages to be displayed. At the start of the routine, this field is set to an empty value. A series of IF statements is then used to compare the field values with a set of validation criteria. If the field does not meet the specified criteria, a warning message is appended to the text string. The Chr$(13) statement is used to separate each warning message by forcing a new line character. After all validation has occurred, the routine checks the state of the text message field. If this field is empty, all validation is successful and the QuerySave continues. Otherwise, a message box is displayed to the user.

Implementation

To implement this solution, insert the following LotusScript code in the QuerySave event of the form. Replace the FIELDA and FIELDB values with valid field names and set the validation criteria. In the following example, both fields must contain a non-blank value. If either field is blank, a message is appended to a text message field, and the message string is displayed to the user upon completion of the field checks (see Figure 13.13).

Sub Querysave(Source As Notesuidocument, Continue As Variant) Dim s As NotesSession Dim w As NotesUIWorkspace Dim uidoc As NotesUIDocument Dim doc As NotesDocument Dim db As NotesDatabase Dim MsgText As String Set s = New NotesSession Set w = New NotesUIWorkspace Set db = s.CurrentDatabase Set uidoc = w.CurrentDocument Set doc = uidoc.Document '-------------------------------------- ' Begin validation '-------------------------------------- Continue = True MsgText = "" If doc.FIELDA(0) = "" Then MsgText=MsgText + "Specify a value for field A." + Chr$(13) End If If doc.FIELDB(0) = "" Then MsgText=MsgText + "Specify a value for field B." + Chr$(13) End If If MsgText <> "" Then Msgbox MsgText, 16, "Required Fields." Continue = False End If End Sub

Figure 13.13. Example message produced by a field validation routine

Категории