Subversion Repositories DevTools

Rev

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