« Thanks for the Plug Bruce! | Main| It's a Boy! »

Modular Approach to Notes Form Validation

QuickImage Category



By popular demand, my first Show-n-Tell Thursday post will be about a modular document validation routine that can be dropped into any form, read the validation errors that have been coded into a specific field on that form, and present a single user-friendly dialog box with ALL entry errors for that document.  This particular code is for the Notes Client only, but the solution can be tailored to a variety of uses.  For example you can, on a form by form basis, decide whether to strictly enforce validation and not allow the user to save without fixing all errors, or instead allow documents to be saved in a "draft" status with incomplete information (good for avoiding users entering junk data just to save the document). So, without further ado...


Step 1
- Put three fields on the form, somewhere near the bottom, as follows:


EnforceValidation
: Text - Computed For Display, with a value of either "Yes" or "No", based on the form requirements.

ValidationMsg
: Text - Computed.  This is where all of the validation logic is placed.  The idea is that for each error, you generate a text line item (e.g. @if(Subject = ""; "- Subject field is required." + @Newline; "") )

ValidationErrorField
: Text - Computed.  This field resolves to the name of the first field with an error, which is where we want to drop the cursor after the error message is displayed on save (just like the way a regular Input Validation works).  The formula might be something like @If(Title = ""; "Title"; Category = ""; "Category"; "")


Step 2
- Creata a subform, name it something like "ValidationSubform", and put the following code in the QuerySave event:



Sub
Querysave(Source As Notesuidocument, Continue As Variant)
Dim
doc As NotesDocument
Dim
err_msg As String
Dim
fieldflag As String    'to indicate the first field on the form which is missing required information
Dim
flag As Integer

Call
Source.Refresh
Set
doc = source.Document

If
Source.fieldgettext("ValidationMsg") <> "" Then
     
err_msg = doc.ValidationMsg(0)
     
fieldflag = doc.ValidationErrorField(0)
End If
     
If
err_msg = "" Then
     
Continue = True
      Goto
FINISH
Else  
'build complete error message and then present Messagebox
       If
Source.fieldgettext("EnforceValidation") = "No" Then
               'Present a Yes/No/Cancel Messagebox

               
err_msg = "The following Information is missing or entered incorrectly:" & Chr(10) & Chr(10) & err_msg
             
err_msg = err_msg & "Do you wish to enter it now?"        'allow fields to bring in extra line return
               
err_msg = err_msg & Chr(10) & Chr(10) & "Click YES to stop and make corrections now" & Chr(10)
             
err_msg = err_msg & "Click NO to save entered information and complete later"
               
flag = Messagebox (err_msg, 3 + 16 + 0, "Validation Failed")
             
If flag = 6 Then     'Yes , stop here and enter enter missing Information (this might generate a Save Action error if the save was triggered by Lotusscript)
                                 
Call source.gotofield(fieldflag)   'place cursor at the first point where information is missing
                                   
Continue = False
               
Elseif flag = 2 Then  'Cancel Save and leave cursor where it is  (this will generate a Save Action error)
                     
Messagebox "Save Cancelled"
                     
continue = False
             
Elseif flag = 7 Then          'NO, do not fill in missing Information
                      'If you're form has some sort of "draft" or "not approved" status, this would be the place to set it

                       
continue = True
     
                   End If
       
ElseIf Source.fieldgettext("EnforceValidation") = "Yes" Then
             
err_msg = "The following Information is missing or entered incorrectly:" & Chr(10) & Chr(10) & err_msg
     
        err_msg = err_msg & Chr(10) & "You must enter the all data correctly in order to save this document."        'allow fields to bring in extra line return
             
flag = Messagebox (err_msg, 16, "Validation Failed")                'OK
               
If fieldflag <> "" Then
     
        Call source.gotofield(fieldflag)   'place cursor at the first point where information is missing        
             
End If
     
        Continue = False
     
End If
End If


FINISH:

' Use Goto function to jump to this point, skipping validation

End Sub


Please note that the code presented here was tweaked somewhat after being pasted into this entry and not retested, so there is always a chance it will break in actual use.  Please let me know if you have any problems or just have questions on something I didn't explain well enough.  I'd also be interested in any suggestions for improvements or alternate approaches.

Comments

1 - Thanks for the improvements Jorn (sorry I don't know how to write your name correctly). If you email me your full code I can update it here. I should probably get into the habit of using lsconst.lss.

2 - Hi Viral,

Nice to hear from you again. I didn't see you at the meeting but that's probably just because I got there so late.

I'm glad you find the code useful. We will be able to talk more about it in March as I certainly plan on attending the meeting, especially since OpenNTF's Bruce Elgort will be our guest.

See you then.

Kev

3 - Great tip!

I've used this technique myself in the past, and it works very well. It's much nicer to be able to present one list of items than to give a series of prompts.

I first used this in R4. The version that I was using got a little convoluted over time - it was "stock code", with fixed fieldnames and structure so that we could re-use it across entire suites of applications. As you can imagine, with all the little exceptions and bits of strangeness, it quickly became a little larger than what you've presented here...

Seeing a neat pared-down version like this jogs the memory, and I'll certainly be using this again! Thanks.


4 - Amazing Kevin!

This is really a useful piece of code. I have always used validation scripts with hug LOC on individual forms. This technique will definately be useful.

Thanks,
Viral

P.S. I didnt get a chance to say hi to you this time in DCNUG Meeting. Hope to see you there in March meeting.

5 - Hi Kevin,

your overall approach is very nice, I use it now in a small application.
I improved it to some extend. In the EnforceValidation = "No" case, you use a Yes/No/Cancel dialog. I don't see why you do this and just exchanged it with an OK/Cancel dialog. If the user clicks ok, the document is saved even though it is not complete, if he cancels, he can complete the document. I think thats much easier to understand.
In addition, I replaced the numbers for the MessageBox with constants.
To use them, you need to put %INCLUDE "lsconst.lss" into (Globals)ValidationSubform under (Declarations).

The code looks now something like this:
flag = Messagebox (err_msg, MB_OKCANCEL, "Validation Failed")
If flag = IDCANCEL Then...

6 - Hi there, thanks for putting this up. Can you explain to a complete notes novice how to code the validationmsg field. at the moment I have this
@If(function= ""; "- function field is required." + @NewLine;
seating="";"-seating field is required." + @NewLine;
"");

the problem with this is I get two prompt messages for each missing field. sorry for such a lame question, I'm very new to notes development.

Thank you

7 - sorry, forget my last comment.
thank you

Your Host

KevinPettitt.jpg
Kevin Pettitt View Kevin Pettitt's profile on LinkedIn

Tools I Use

Idea Jam

Subscribe to This Blog

 Full Posts  Comments

MyYahoo
netvibes Add to Netvibes

Contact

Hosted by

OpenNTF

Disclaimer

This site is in no way affiliated, endorsed, sanctioned, supported, nor blessed by Lotus Software nor IBM Corporation, nor any of my past or future clients (although they are welcome to do so). The opinions, theories, facts, etc. presented here are my own and in no way represent any official pronouncement by me on behalf of any other entity.

© 2005-2017 Kevin Pettitt - all rights reserved as listed below.

Creative Commons License
Unless otherwise labeled by its originating author, the content found on this site is made available under the terms of an Attribution / NonCommercial / ShareAlike