Subversion Repositories DevTools

Rev

Rev 5506 | Rev 5658 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
119 ghuddy 1
<%
2
'=============================================================
3
'//
129 ghuddy 4
'//                  Validation Control
119 ghuddy 5
'//
6
'=============================================================
7
%>
8
<%
9
Class ValidationControl
129 ghuddy 10
 
11
   Private maRules()
12
   Private mobjFieldMap
13
   Private mobjErrorMsg
14
   Private mobjAltVal
15
 
16
   Private InxFieldName
17
   Private InxIsRequired
18
   Private InxIsNumeric
19
   Private InxMinNumericValue
20
   Private InxMaxNumericValue
21
   Private InxIsDate
22
   Private InxStartDate
23
   Private InxEndDate
24
   Private InxMinStringLength
25
   Private InxMaxStringLength
26
   Private InxRegExp
27
   Private InxRegExpDescription
28
 
29
   Private mNumOfRules
30
   Private mLastRuleInx
31
 
32
   Private bIsFormValid
33
   Private bIsValidated
34
   Private bIsPostBack
35
   Private bHiddenTagPlanted
36
 
37
   Private sPostBackTagName
38
   Private sBULET
39
 
40
 
41
   Public Property Get IsPostBack ()
42
      IsPostBack = bIsPostBack
43
   End Property
44
 
45
   Public Property Get IsValidOnPostBack
46
      If bIsValidated Then
47
         IsValidOnPostBack = bIsFormValid
48
      Else
49
         IsValidOnPostBack = ValidateForm()
50
      End If
51
 
52
   End Property
53
 
54
   '-----------------------------------------------------------------------------------------------------------------
55
   Public Function GetValue ( sFieldName, altValue )
56
      If NOT bIsPostBack Then
57
         GetValue = altValue
58
      Else
59
         GetValue = Request(sFieldName)
60
      End If
61
 
62
      mobjAltVal.Item ( sFieldName ) = GetValue
63
   End Function
64
   '-----------------------------------------------------------------------------------------------------------------
65
   Public Sub SetValue ( sFieldName, altValue )
66
      If NOT bIsPostBack Then
67
         mobjAltVal.Item ( sFieldName ) = altValue
68
      Else
69
         mobjAltVal.Item ( sFieldName ) = Request(sFieldName)
70
      End If
71
 
72
   End Sub
73
   '-----------------------------------------------------------------------------------------------------------------
74
   Public Sub SetValueForced ( sFieldName, value )
75
      mobjAltVal.Item ( sFieldName ) = value
76
 
77
   End Sub
78
   '-----------------------------------------------------------------------------------------------------------------
79
   Private Function RequestValue( sFieldName )
80
      If NOT bIsPostBack Then
81
         RequestValue = mobjAltVal.Item ( sFieldName )
82
      Else
83
         If Request(sFieldName) <> "" Then
84
            RequestValue = Request(sFieldName)
85
         Else
86
            RequestValue = mobjAltVal.Item ( sFieldName )
87
         End If
88
      End If
89
 
90
   End Function
91
   '-----------------------------------------------------------------------------------------------------------------
92
   Public Function IsTicked( sFieldName, nParId, altValue )
93
      Dim sParList
94
 
95
      nParId     = ","&  Replace(nParId, " ", "") &","
96
      sParList = ","&  Replace( Request(sFieldName), " ", "") &","
97
 
98
      If NOT bIsPostBack Then
99
         IsTicked = (NOT IsNull(altValue))  OR  (altValue <> "")
100
 
101
      Else
102
         If InStr( sParList, nParId ) > 0 Then
103
            IsTicked = TRUE
104
         Else
105
            IsTicked = FALSE
106
         End If
107
 
108
      End If
109
 
110
   End Function
5596 dpurdie 111
 
129 ghuddy 112
   '-----------------------------------------------------------------------------------------------------------------
5596 dpurdie 113
   Public Function AddPostBack ()
129 ghuddy 114
      '--- Plant Hidden Tag
115
      If NOT bHiddenTagPlanted Then
116
         ' This tag is used by this class to know if the form is posted back
117
         Response.write "<input type='hidden' name='"& sPostBackTagName &"' value='true'>"
118
         bHiddenTagPlanted = TRUE
119
      End If
5596 dpurdie 120
   End Function
129 ghuddy 121
 
5596 dpurdie 122
   '-----------------------------------------------------------------------------------------------------------------
123
   Public Function Validate ( sFieldName )
124
 
125
      '--- Plant Hidden Tag
126
      AddPostBack()
127
 
129 ghuddy 128
      Call ValidateField ( RequestValue(sFieldName), mobjFieldMap.Item ( Cstr( sFieldName  ) ) )
129
 
130
      Validate = GetErrorMsg ( sFieldName )
131
 
132
   End Function
133
   '-----------------------------------------------------------------------------------------------------------------
134
   Private Function ValidateForm()
135
      Dim i, FieldValue, nLastRowInx
136
      mobjErrorMsg.RemoveAll   ' Clean Error Messages
137
 
138
      nLastRowInx = LastRowInx ()
139
 
140
      For i = 0 To nLastRowInx
141
         FieldValue = RequestValue( maRules( InxFieldName, i ) )
142
 
143
         Call ValidateField ( FieldValue, i )
144
 
145
      Next
146
 
147
      ' --- Finally, set the Form state of validity
148
      If mobjErrorMsg.Count > 0 Then
149
         bIsFormValid = FALSE
150
      Else
151
         bIsFormValid = TRUE
152
      End If
153
 
154
 
155
      bIsValidated = TRUE
156
      ValidateForm = bIsFormValid
157
 
158
   End Function
159
   '-----------------------------------------------------------------------------------------------------------------
160
   Private Sub ValidateField ( FieldValue, i )
161
 
162
      If (i = "") OR (IsNull(i)) Then
163
         Err.Raise 8, "Cannot Find Field Name.", "Make sure you have correct filed names listed for validation."
164
         Exit Sub
165
      End If
166
 
167
      If mobjErrorMsg.Exists ( CStr( maRules( InxFieldName, i ) ) ) Then mobjErrorMsg.Remove ( CStr( maRules( InxFieldName, i ) ) )   ' Clean this field Error Messages
168
 
169
      ' RULE is_Required
170
      If NOT ValidForIsRequired ( FieldValue, i ) Then
171
         Call AddErrorMessage ( i, "Required." )
172
 
173
      Else
174
 
175
         If FieldValue <> "" Then
176
 
177
            ' --- RULE is_Number ---
178
            If NOT ValidForIsNumeric ( FieldValue, i ) Then
179
               Call AddErrorMessage ( i, "Must be a number." )
180
            Else
181
 
182
               If maRules( InxIsNumeric, i ) = enumDB_YES Then  ' Continue if field is a Number
183
                  ' --- RULE min_Numeric_value ---
184
                  If NOT ValidForMinNumericValue ( FieldValue, i ) Then
185
                     Call AddErrorMessage ( i, "Must be minimum "& maRules( InxMinNumericValue, i ) &"." )
186
                  End If
187
 
188
                  ' --- RULE max_Numeric_value ---
189
                  If NOT ValidForMaxNumericValue ( FieldValue, i ) Then
190
                     Call AddErrorMessage ( i, "Must be maximum "& maRules( InxMaxNumericValue, i ) &"." )
191
                  End If
192
               End If
193
 
194
            End If
195
 
196
 
197
            ' --- RULE is_Date ---
198
            If NOT ValidForIsDate ( FieldValue, i ) Then
199
               Call AddErrorMessage ( i, "Must be a date." )
200
            Else
201
 
202
               If maRules( InxIsDate, i ) = enumDB_YES Then  ' Continue if field is a Date
203
                  ' --- RULE start_Date ---
204
                  If NOT ValidForStartDate ( FieldValue, i ) Then
205
                     Call AddErrorMessage ( i, "Cannot be before "& maRules( InxStartDate, i ) &"." )
206
                  End If
207
 
208
                  ' --- RULE end_Date ---
209
                  If NOT ValidForEndDate ( FieldValue, i ) Then
210
                     Call AddErrorMessage ( i, "Cannot be after "& maRules( InxStartDate, i ) &"." )
211
                  End If
212
 
213
               End If
214
 
215
            End If
216
 
217
 
218
            ' --- RULE min_String_Length ---
219
            If NOT ValidForMinStringLength ( FieldValue, i ) Then
220
               Call AddErrorMessage ( i, "Must be at least "& maRules( InxMinStringLength, i ) &" character(s) long." )
221
            End If
222
 
223
            ' --- RULE min_String_Length ---
224
            If NOT ValidForMaxStringLength ( FieldValue, i ) Then
225
               Call AddErrorMessage ( i, "Can be maximum "& maRules( InxMaxStringLength, i ) &" character(s) long." )
226
            End If
227
 
228
 
229
            ' --- RULE Regular Expression ---
230
            If NOT ValidForRegExp ( FieldValue, i ) Then
231
               Call AddErrorMessage ( i, maRules( InxRegExpDescription, i ) )
232
            End If
233
 
234
 
235
 
236
         End If
237
 
238
 
239
      End If
240
   End Sub
241
   '-----------------------------------------------------------------------------------------------------------------
242
   Private Function ValidForIsRequired ( fieldValue, rowId )
243
      ValidForIsRequired = FALSE
244
      If maRules( InxIsRequired, rowId ) = enumDB_YES Then
245
         '/* Check if empty */
246
         If fieldValue <> "" Then
247
 
248
            '/* Check for spaces */
249
            If Len( Replace(fieldValue, " ", "") ) > 0 Then   ValidForIsRequired = TRUE
250
 
251
         End If
252
 
253
      Else
254
         ValidForIsRequired = TRUE
255
 
256
      End If
257
   End Function
258
   '-----------------------------------------------------------------------------------------------------------------
259
   Private Function ValidForRegExp ( fieldValue, rowId )
260
      Dim objRegEx
261
 
262
      ValidForRegExp = FALSE
263
      If IsNull( maRules( InxRegExp, rowId ) ) OR (maRules( InxRegExp, rowId ) = "") Then
264
         ValidForRegExp = TRUE
265
      Else
266
         Set objRegEx = New RegExp
267
 
268
         objRegEx.Global = False      ' Only find first match. This is enough to fail the validation
269
         objRegEx.IgnoreCase = False   ' Follow match pattern exactly.
270
          objRegEx.Pattern = maRules( InxRegExp, rowId )       ' Set the pattern to match
271
 
272
         ' Now test the pattern match.
273
         If NOT objRegEx.Test( fieldValue ) Then
274
            ValidForRegExp = TRUE
275
         End If
276
 
277
         Set objRegEx = Nothing
278
      End If
279
   End Function
280
   '-----------------------------------------------------------------------------------------------------------------
281
   Private Function ValidForIsNumeric ( fieldValue, rowId )
282
      ValidForIsNumeric = FALSE
283
      If maRules( InxIsNumeric, rowId ) = enumDB_YES Then
284
         If IsNumeric( fieldValue ) Then ValidForIsNumeric = TRUE
285
 
286
      Else
287
         ValidForIsNumeric = TRUE
288
 
289
      End If
290
   End Function
291
   '-----------------------------------------------------------------------------------------------------------------
292
   Private Function ValidForMinNumericValue ( fieldValue, rowId )
293
      ValidForMinNumericValue = FALSE
294
      If IsNull( maRules( InxMinNumericValue, rowId ) ) OR (maRules( InxMinNumericValue, rowId ) = "") Then
295
         ValidForMinNumericValue = TRUE
296
      Else
297
         If CDbl(fieldValue) >= CDbl(maRules( InxMinNumericValue, rowId )) Then   ValidForMinNumericValue = TRUE
298
 
299
      End If
300
   End Function
301
   '-----------------------------------------------------------------------------------------------------------------
302
   Private Function ValidForMaxNumericValue ( fieldValue, rowId )
303
      ValidForMaxNumericValue = FALSE
304
      If IsNull( maRules( InxMaxNumericValue, rowId ) ) OR (maRules( InxMaxNumericValue, rowId ) = "") Then
305
         ValidForMaxNumericValue = TRUE
306
      Else
307
         If CDbl(fieldValue) <= CDbl(maRules( InxMaxNumericValue, rowId )) Then   ValidForMaxNumericValue = TRUE
308
 
309
      End If
310
   End Function
311
   '-----------------------------------------------------------------------------------------------------------------
312
   Private Function ValidForIsDate ( fieldValue, rowId )
313
      ValidForIsDate = FALSE
314
      If maRules( InxIsDate, rowId ) = enumDB_YES Then
315
         If IsDate( fieldValue ) Then ValidForIsDate = TRUE
316
 
317
      Else
318
         ValidForIsDate = TRUE
319
 
320
      End If
321
   End Function
322
   '-----------------------------------------------------------------------------------------------------------------
323
   Private Function ValidForStartDate ( fieldValue, rowId )
324
      ValidForStartDate = FALSE
325
      If IsNull( maRules( InxStartDate, rowId ) ) OR (maRules( InxStartDate, rowId ) = "") Then
326
         ValidForStartDate = TRUE
327
      Else
328
         If CDate(fieldValue) >= CDate(maRules( InxStartDate, rowId )) Then   ValidForStartDate = TRUE
329
 
330
      End If
331
   End Function
332
   '-----------------------------------------------------------------------------------------------------------------
333
   Private Function ValidForEndDate ( fieldValue, rowId )
334
      ValidForEndDate = FALSE
335
      If IsNull( maRules( InxEndDate, rowId ) ) OR (maRules( InxEndDate, rowId ) = "") Then
336
         ValidForEndDate = TRUE
337
      Else
338
         If CDate(fieldValue) <= CDate(maRules( InxStartDate, rowId )) Then   ValidForEndDate = TRUE
339
 
340
      End If
341
   End Function
342
   '-----------------------------------------------------------------------------------------------------------------
343
   Private Function ValidForMinStringLength ( fieldValue, rowId )
344
      ValidForMinStringLength = FALSE
345
      If IsNull( maRules( InxMinStringLength, rowId ) ) OR (maRules( InxMinStringLength, rowId ) = "") Then
346
         ValidForMinStringLength = TRUE
347
      Else
348
         If CInt(Len(fieldValue)) >= CInt(maRules( InxMinStringLength, rowId )) Then   ValidForMinStringLength = TRUE
349
 
350
      End If
351
   End Function
352
   '-----------------------------------------------------------------------------------------------------------------
353
   Private Function ValidForMaxStringLength ( fieldValue, rowId )
354
      ValidForMaxStringLength = FALSE
355
 
356
      If IsNull( maRules( InxMaxStringLength, rowId ) ) OR (maRules( InxMaxStringLength, rowId ) = "") Then
357
         ValidForMaxStringLength = TRUE
358
      Else
359
         If CInt(Len(fieldValue)) <= CInt(maRules( InxMaxStringLength, rowId )) Then   ValidForMaxStringLength = TRUE
360
 
361
      End If
362
   End Function
363
   '-----------------------------------------------------------------------------------------------------------------
364
   Private Sub AddErrorMessage ( rowId, sErrMsg )
365
      mobjErrorMsg.Item (Cstr( maRules( InxFieldName, rowId ) )) = _
366
         mobjErrorMsg.Item (Cstr( maRules( InxFieldName, rowId ) )) &_
367
         "<tr>"&_
368
         sBULET  &"<td class='val_err'>"&  sErrMsg  &"</td>"&_
369
         "</tr>"& VBNewLine
370
   End Sub
371
   '-----------------------------------------------------------------------------------------------------------------
372
   Public Sub LoadFieldRules ( aRows )
373
      ' Pass the full array row matching the columns of maRules()
374
      Dim nProperty, newArrayDim, numOfRows, rowNum
375
 
376
      numOfRows = UBound( aRows, 2 )
377
 
378
 
379
      For rowNum = 0 To numOfRows
380
         ' Increase array by 1
381
         newArrayDim = LastRowInx() + 1
382
         ReDim Preserve maRules( mNumOfRules, newArrayDim )
383
 
384
         mobjFieldMap.Add ( Cstr( aRows ( InxFieldName, rowNum ) ) ), CStr( newArrayDim )
385
 
386
         For nProperty = 0 To mLastRuleInx
387
            maRules ( nProperty, newArrayDim ) = aRows ( nProperty, rowNum )
388
         Next
389
 
390
      Next
391
 
392
      ' --- Validate Form ---
393
      'ValidateForm()
394
 
395
   End Sub
396
   '-----------------------------------------------------------------------------------------------------------------
397
   Private Function LastRowInx ()
398
       LastRowInx = UBound ( maRules, 2 )
399
   End Function
400
   '-----------------------------------------------------------------------------------------------------------------
401
   Private Function GetErrorMsg ( sFieldName )
402
      Dim msg
403
      msg = mobjErrorMsg.Item (CStr(sFieldName))
404
      If msg <> "" Then
405
         GetErrorMsg = _
406
         "<table width='100%'  border='0' cellspacing='2' cellpadding='0'>"& VBNewLine &_
407
         msg & VBNewLine &_
408
         "<tr><td width='1'>"& SPACER &"</td><td width='1'>"& SPACER &"</td><td width='100%'>"& SPACER &"</td></tr>"& VBNewLine &_
409
         "</table>"
410
      Else
411
         GetErrorMsg = NULL
412
      End If
413
   End Function
414
   '-----------------------------------------------------------------------------------------------------------------
415
   Private Function ParseParams ( sParams )
416
      Dim paramArr, i
417
 
418
      paramArr = Split ( sParams, "'" )      ' Expected Params value:  id='field_name' IsRequired='N' param2='val' ...
419
 
420
      ' Store Validation changes/rules in array
421
      For i = 0 To UBound( paramArr )-1 Step 2
422
         Call UpdateRow ( paramArr(1), GetColumnInx ( paramArr(i) ), paramArr(i+1) )
423
      Next
424
 
425
      ParseParams = paramArr(1)   ' id must be first param
426
 
427
   End Function
428
   '-----------------------------------------------------------------------------------------------------------------
429
   Public Sub UpdateRules ( sParams )
430
      ParseParams ( sParams )
431
 
432
   End Sub
433
   '-----------------------------------------------------------------------------------------------------------------
434
   Private Sub UpdateRow ( sFieldName, sColumnInx, sColumnVal )
435
      Dim rowNum
436
 
437
      If mobjFieldMap.Exists (CStr(sFieldName)) Then
438
         rowNum = mobjFieldMap.Item ( Cstr( sFieldName  ) )
439
 
440
      Else
441
         rowNum = LastRowInx() + 1
442
         ReDim Preserve maRules( mNumOfRules, rowNum )
443
 
444
         mobjFieldMap.Add ( Cstr( sFieldName ) ), CStr( rowNum )
445
 
446
      End If
447
 
448
      maRules ( sColumnInx, rowNum ) = sColumnVal
449
 
450
   End Sub
451
   '-----------------------------------------------------------------------------------------------------------------
452
   Private Function GetColumnInx ( ByVal sParam )
453
      sParam = Trim( sParam )      ' Trim spaces
454
      sParam = Left( sParam, Len(sParam)-1 )   ' Remove trailing "="
455
 
456
      Select Case Trim( sParam )
457
         Case "id"
458
            GetColumnInx = InxFieldName
459
         Case Else
460
            GetColumnInx = Eval( "Inx"& Trim( sParam ) )
461
 
462
      End Select
463
   End Function
464
   '-----------------------------------------------------------------------------------------------------------------
465
   Sub LoadValidationRules ( aFieldList, ByRef objOraDatabase )
466
      Dim rsQry, query
467
 
468
      query = _
469
      "   SELECT FIELD_NAME, "&_
470
      "         IS_REQUIRED, "&_
471
      "         IS_NUMERIC, "&_
472
      "         MIN_NUMERIC_VALUE, "&_
473
      "         MAX_NUMERIC_VALUE, "&_
474
      "         IS_DATE, "&_
475
      "         START_DATE, "&_
476
      "         END_DATE, "&_
477
      "         MIN_STRING_LENGTH, "&_
478
      "          MAX_STRING_LENGTH, "&_
479
      "         REGEXP, "&_
480
      "         REGEXP_DESCRIPTION "&_
481
      "     FROM VALIDATION_RULES"&_
482
      "    WHERE field_name IN ( '"& Join( aFieldList, "','") &"' )"
483
 
484
 
485
      Set rsQry = objOraDatabase.DbCreateDynaset( query , 0 )
486
      If ((NOT rsQry.BOF) AND (NOT rsQry.EOF)) Then
487
         Call LoadFieldRules ( rsQry.GetRows() )
488
 
489
      End If
490
 
491
      rsQry.Close
492
      Set rsQry = Nothing
493
   End Sub
494
   '-----------------------------------------------------------------------------------------------------------------
495
   Private Sub Class_Initialize()
496
      '// Perform action on creation of object. e.g. Set myObj = New ThisClassName
497
      Set mobjFieldMap = CreateObject("Scripting.Dictionary")
498
      Set mobjErrorMsg = CreateObject("Scripting.Dictionary")
499
      Set mobjAltVal     = CreateObject("Scripting.Dictionary")
500
 
501
      sPostBackTagName = "VC_POST_BACK"
502
 
503
 
504
      bHiddenTagPlanted = FALSE
505
      bIsPostBack = FALSE      ' When true, form has been submitted and need postback validation
506
      bIsValidated = FALSE
507
 
508
 
5596 dpurdie 509
      InxFieldName          = 0
129 ghuddy 510
      InxIsRequired         = 1
5596 dpurdie 511
      InxIsNumeric          = 2
512
      InxMinNumericValue    = 3
513
      InxMaxNumericValue    = 4
514
      InxIsDate             = 5
515
      InxStartDate          = 6
129 ghuddy 516
      InxEndDate            = 7
5596 dpurdie 517
      InxMinStringLength    = 8
518
      InxMaxStringLength    = 9
519
      InxRegExp             = 10
520
      InxRegExpDescription  = 11
521
      mNumOfRules           = 12     ' Number of Rules that can be assigned to one field
129 ghuddy 522
      mLastRuleInx = mNumOfRules - 1
523
 
524
      ReDim maRules ( mNumOfRules, -1 )
525
 
526
      'sSESSION_SEPARATOR = "|SEPARATOR|"      ' Make sure it will never show in regexp field
4028 dpurdie 527
      sBULET = "<td>"& SPACER &"</td>"&_
129 ghuddy 528
             "<td valign='top'><img src='icons/i_bulet_red.gif' width='4' height='4' hspace='3' vspace='4' border='0' align='absmiddle'></td>"
529
 
530
 
531
      '--- Check if Form is posted back
532
      If Request(sPostBackTagName) <> "" Then
533
         bIsPostBack = TRUE
534
      End If
535
   End Sub
536
   '-----------------------------------------------------------------------------------------------------------------
537
   Private Sub Class_Terminate()
538
      '// Perform action on object disposal. e.g. Set myObj = Nothing
539
      Set mobjFieldMap = Nothing
540
      Set mobjErrorMsg = Nothing
541
      Set mobjAltVal = Nothing
542
 
543
   End Sub
544
   '-----------------------------------------------------------------------------------------------------------------
119 ghuddy 545
End Class
129 ghuddy 546
%>