在开发传统Notes客户机应用时,校验是最常见的功能需求之一。在检查一张表单的输入时,能够使用的方法和呈现给用户的方式很有限。一般我们不会使用域的输入验证公式,因为那样做太分散、重复又不够灵活。更好的方案是将所有的检查集中在一起,在文档保存或者执行某个操作时调用。校验有可能包含对输入做各种检查,最普遍的还是简单的非空性检查。对此,我们可以写一个简单的校验类:
Private Const MESSAGE="Please input the field "
Public Class Validator
Private fields List As String
Private m_uidoc As NotesUIDocument
Private m_doc As NotesDocument
Public Sub new()
Dim ws As New NotesUIWorkspace
Set m_uidoc=ws.CurrentDocument
Set m_doc=m_uidoc.Document
End Sub
Public Sub Add(fieldName As String,label As String)
If label="" Then
label=fieldName
End If
fields(label)=fieldName
End Sub
Public Function Validate() As Boolean
Forall f In fields
If m_uidoc.FieldGetText(f)="" Then
Messagebox MESSAGE & {"} & Listtag(f) & {"},64,"Lotus Notes"
If m_uidoc.EditMode Then
Call m_uidoc.GotoField(f)
End If
Validate=False
Exit Function
End If
End Forall
Validate=True
End Function
End Class
在这个类中,Add()方法像要检查的域名和对应的对用户的名称添加到一个List中。Validate()方法检查这些域的值是否为空;如果是,则给用户一个提示,将焦点转移到该域中,并返回False;如果所有域值都不为空,就返回True。这样调用的程序就可以根据Validate()方法的结果判断是否继续执行以后的逻辑。
下面这段代码就是在一个文档的Querysave事件中根据条件添加了若干个需要校验的域,并且根据校验的结果决定是否保存文档。
Sub Querysave(Source As NotesUIDocument, Continue As Variant)
Dim validator1 As New Validator()
With validator1
Call .Add("ActionName","Action Name")
Call .Add("NodeName","Node Name")
If Source.FieldGetText("MultipleNext")="" Then
Call .Add("NextNode","Next Node")
End If
If source.FieldGetText("NeedExpression")><"" Then
Call .Add("Expression", "Action Expression")
End If
If Source.FieldGetText("NeedMail")="1" Then
Call .Add("Subject","Mail Subject")
End If
End With
If Not validator1.Validate() Then
Continue=False
End If
End Sub
上面的校验都是针对普通域,如果要校验一个富文本域(RichTextItem)是否包含附件,就必须先保存当前Notes文档,然后可以使用下列函数:
Public Function CheckRTFAttachment(curDoc As NotesDocument,fieldName As String) As Boolean
'fieldnum is the index of RTF item name in the CheckItems array
CheckRTFAttachment=False
Dim vChkItem As Variant
Dim vObject As Variant
Dim intObject As Integer
Set vChkItem=curDoc.GetFirstItem(fieldName)
vObject=vChkItem.EmbeddedObjects
If Not IsEmpty(vObject) Then
For intObject=0 To UBound(vObject)
If vObject(intObject).Type=1454 Then
'1454 means attachment
CheckRTFAttachment=True
Exit Function
End If
Next
End If
End Function
函数参数中的curDoc为包含要校验的富文本域的文档,fieldName为域名。如果包含附件则返回True;反之False。