SnTT : LotusScript Required Field Validation without pain Revisted.

As mentioned at the end of last thursdays , the feed back to Required Field Validation tip caused me to rethink and revise. Many thanks to by for kicking my arse nudging my own thinking.

The first step is having a computed for display field of the form ListOfRequiredFields, multi – values, which content like :

“FirstName|First Name”:
“phone|Telephone Number (inculde area code)”:
“Province|Province or State”

(and so on) the point being “fieldName| Field Label” colon to the next one. You could also have @if formula’s to control the values, for example :

for differnet each new status in a work flow : @if(Status=”Pending”;”Value|Total Budget Amount ($)”; “”)

or conditional required field labels : @if (country=”USA”; “Province|State”;”Province|Province”)

you could (as Theo hinted) have the value for the field comming from a profile document.

Now the meat : much simpler QuerySave placed on the form with the field list :

1
2
3
4
5
6
7
8
9
10
11
12
Sub Querysave(Source As Notesuidocument, Continue As Variant)
    Dim doc As NotesDocument
    Set doc = Source.Document
     dim FormLabel as String
    FormLabel = "Admin Profile"
    If  ValidateForm ( Doc, FormLabel, "ListOfRequiredFields"  )Then
        Continue = True
    Else
        Continue = False
        Exit Sub
    End If
End Sub

And in either the forms global section or in a library 2 functions (Note you will have to do a “%INCLUDE “LSCONST.LSS”” somewhere, as well):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
Function ValidateForm (Doc As NotesDocument, FormName As String, FNforLoR As String ) As Integer

    On Error Goto processError    

    Const NoCaseNoPitch = 5
    Dim errPre As String
    Dim errMsg As String
    Dim msgTitle As String
    Dim LoRitem As NotesItem  ' List of Required Fields item
    Dim pos As Integer
    Dim FieldName As String
    Dim FieldLabel As String
    Dim CRLF As String

        Set LoRitem = Doc.GetFirstItem (FNforLoR)
        If LoRitem Is Nothing Then
              ValidateForm  = True
              Exit Function
        Elseif  LoRitem.text = ""  Then  ' Nothing To test
              ValidateForm  = True
              Exit Function
        End If

    CRLF= Chr(13)' & Chr$(10)
    msgTitle = "Validation Error saving " + FormName
    errPre ="The following field(s) are blank , or incorrect, and must be inputted before saving:" & CRLF & CRLF
    ErrMsg = ""

    Forall V In LoRitem.Values
        pos = Instr (1, V, "|" ,NoCaseNoPitch )
        FieldName = Trim( Left( V, pos - 1))
        FieldLabel =   Trim(Mid(V, pos +1) )
        If  isVaildateFieldEmpty ( Doc,  FieldName) Then
            ErrMsg = errMsg  & CRLF & FieldLabel
        End If
    End Forall

    If Not errMsg = ""  Then
        Messagebox   errPre & errMsg, MB_OK+MB_ICONSTOP, msgTitle
        ValidateForm  = False
    Else
        ValidateForm  = True
    End If
    Exit Function

processError:
    Messagebox "System Error " & Err() & ": " & Error$  & ", line: " & Erl
    ValidateForm = False

End Function
Function isVaildateFieldEmpty (Doc As NotesDocument , FieldName As String ) As Integer
' pass in the document to be validated, and the FieldName

    If Not doc.hasitem(FieldName) Then ' if the field is not on the document report that as being empty
        isVaildateFieldEmpty = True
        Exit Function
    End If
    If doc.GetFirstItem (FieldName).text = "" Then
        isVaildateFieldEmpty = True
    Else
        isVaildateFieldEmpty = False
    End If

End Function

I expect you could also whip up fields and functions to check IsFieldNumeric, IsFieldTelephone, and a bunch of other common needs. (I hope Theo approves!)

Update (Dec 18 2006) prompted by some questions (thanks Rachel V!), and the need to “eat my own dog food”, I’ve moved the routines into a text file (so you can import into a script library, or copy aand paste with a lot less pain), and made a few other changes (including testing if the field does exist -thanks Theo!-, and changes to make to it debugging friendly, etc.), plus a lot more testing : validatefields.txt.

(technorati.com tags :, )

3 Replies to “SnTT : LotusScript Required Field Validation without pain Revisted.”

  1. Well you could take a much more sophisticated approach from lotus script, if you let the user do the validation configuration. !!Help!! does it this way. With much more then is blank ….

  2. Wow, much cleaner and re-useable code !

    A consistent typo though: VaildateFieldNotEmpty 😉

    One thing I would change, is checking if the field does exist !
    so instead of :
    doc.GetFirstItem (FieldName).text = “”
    I’ld add:
    if doc.hasitem(FieldName) then……

    [Thanks Theo. I still think “” is what I meant. Question if the field doess not exit do you throw an error, or keep going (since it is the developer who made the error)?]

  3. Pingback: False Positives » Blog Archive » SNTT : Test for Unique values in a Lotus Notes Form field

Leave a Reply