Updated : Revisited with 300% less Pain! and much better code.
I’m missed the last couple of weeks but I’m back for another Show-n-Tell+Thursday, and doing a followup on my first post Building a better “Save & Exit†action button in Lotus Notes formula language, and the disscsion that in generated and doing required field Validation without pain. What I wanted some some good, but quick way to validate that the required field have been filled.
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 65 66 67 68 69 | Sub Querysave(Source As Notesuidocument, Continue As Variant) Dim doc As NotesDocument Set doc = Source.Document Dim fieldList(1 To 130, 1 To 2) As String fieldList (1,1) ="FirstName" ' Field Name fieldList (1,2) ="First Name" ' Field Label fieldList (2,1) ="LastName" fieldList (2,2) ="Last Name" fieldList (3,1) ="StreetAddress" fieldList (3,2) ="Street Address" If ValidateForm ( Doc, "Admin Profile", FieldList )Then Continue = True Else Continue = False End If End Sub %INCLUDE "LSCONST.LSS" Function ValidateForm (Doc As NotesDocument, FormName As String, FieldList As Variant ) As Integer On Error Goto processError Dim errPre As String Dim errMsg As String Dim msgTitle As String msgTitle = "Validation Error saving " + FormName + " Request" errPre ="The following field(s) are blank , or incorrect, and must be inputted before saving:" & CRLF & CRLF 'Dim item As notesItem Dim x As Integer For x = 1 To Ubound ( FieldList, 1) ErrMsg = errMsg + VaildateFieldNotEmpty (Doc,FieldList(x,1), FieldList(x,2) ) Next 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 Exit Function End Function Function VaildateFieldNotEmpty (Doc As NotesDocument , FieldName As String , Msg As String) As String Dim CRLF As String CRLF= Chr$(13) If doc.GetFirstItem (FieldName).text = "" Then VaildateFieldNotEmpty = CRLF+Msg Else VaildateFieldNotEmpty = "" End If End Function |
So the first routine is the QuerySave, on the form, which bulds array of FieldName and FieldLabels to check for very basic required field validatation.
The second and third routines could be on the form but should be in a script library. The
%INCLUDE “LSCONST.LSS” needs to be in the declaration section.
The third routine takes the Document and FieldName and tests to see if the field is empty, if it is then it passes back the fields label (or it could be another message). My test seems to work for most fields in my – brief – testing, but some work is need for more robost testing.
The second routine, loops though the passed array and builds a string of field labels (or messages) and if not empty display a dialog box with one list of field requiring content. It also tell the QuerySave that it failed.
All this works in conjuction with my earlier : @If(@Command([FileSave]);@Command([FileCloseWindow]);—)
It also will work with any induvuial field Valiation, which get checked after the QuerySave if that is successful. You could also do more completed validation (of the A must be greater that B, or C must be odd) after this, in the query save.
If your doing Domino web work take a look at Rich’s SnTT 1: Easy JS Validation on a Form, although (evil thought) I wonder if something like this could work in the notes client javascript support? You still need back end validate though. (which leads to another evil thought – 2 in one morning? !!) about using RSJ templates to generante the javascript for validatation based on the RubyOnRails meta data … hmm)
another update : Chris Blatnick has posted a javascript based form validation in SnTT: Good Looking and Useable Form Validation , the the comments (below) by Theo Heselmans have nudged by own thinking. A mild revision to follow.
Rather then using a 2 dimensional array, wouldn’t it be easier to use lists to store the fieldname and label? Then rather then using ubounds which I believe are expensive to process you could use a forall loop….
Just a thought…
[Ian : using a List would mean not worrying about the lenght, when you need to add or remove a field to check, and the syntax might be less awkward as well. I had not heard he about ubounds being expensive but it’s possible. all in all some thing it look in to. Many Thanks!]
Wouldn’t it be easier to put 2 multi-value computed for display fields on each form with a predifined name, e.g. Check_FieldName and Check_FieldLabel.
This way your validation function could be the same on each form, and the list can be easily adapted, without digging into the code.
(You could even use 1 field, with for each field e.g. “FName|First Name”).
[Ian: I like the idea of one field, and then pass the field name (something meaningful Like “ListofRequiredFields”) to the function. You still have have code to dig in to, but at a lessor level. Question, what type of field would be best : Computed, for Dispay, or hidden editable?]
The ‘universal code’ could look for the existance of a field ‘ListofRequiredFields’. If it exists, it would do validation, otherwise it doesn’t.
Your code does not need to be touched from one form to the other (you clould put it in a script or a header subform you use on every form.
Regarding the type of the field: Computer for Display for sure !
What I usally do is compute the field based on a parameter document (or a databae profile field). This way a db administrator can (re-)define the required fields without developer interference !
My parameter docs usually are called ‘Required_Company’ or ‘Required_Project (one for each form).
The advantage is that you can build quit elaborate validation rules.
My ‘Required_Project’ rule contains:
Signer
Source
@if(QuoteDate
Pingback: False Positives » Blog Archive » SnTT : LotusScript Required Field Validation without pain Revisted.