|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- <%
- '=======================================================================================================================
- ' Validation Classes
- '=======================================================================================================================
-
- '-----------------------------------------------------------------------------------------------------------------------
- ' Exists Validation
- '-----------------------------------------------------------------------------------------------------------------------
- Class ExistsValidation_Class
- Private m_instance
- Private m_field_name
- Private m_message
- Private m_ok
-
- Public Function Initialize(instance, field_name, message)
- set m_instance = instance
- m_field_name = field_name
- m_message = message
- m_ok = true
- set Initialize = Me
- End Function
-
- Public Sub Check
- If Len(eval("m_instance." & m_field_name)) = 0 then
- m_ok = false
- End If
- End Sub
-
- Public Property Get OK
- OK = m_ok
- End Property
-
- Public Property Get Message
- Message = m_message
- End Property
- End Class
-
- Sub ValidateExists(instance, field_name, message)
- if not IsObject(instance.Validator) then set instance.Validator = new Validator_Class
- instance.Validator.AddValidation new ExistsValidation_Class.Initialize(instance, field_name, message)
- End Sub
-
-
- '-----------------------------------------------------------------------------------------------------------------------
- ' Minimum Length Validation
- '-----------------------------------------------------------------------------------------------------------------------
- Class MinLengthValidation_Class
- Private m_instance
- Private m_field_name
- Private m_size
- Private m_message
- Private m_ok
-
- Public Function Initialize(instance, field_name, size, message)
- set m_instance = instance
- m_field_name = field_name
- m_size = size
- m_message = message
- m_ok = true
- set Initialize = Me
- End Function
-
- Public Sub Check
- If Len(eval("m_instance." & m_field_name)) < m_size then m_ok = false
- End Sub
-
- Public Property Get OK
- OK = m_ok
- End Property
-
- Public Property Get Message
- Message = m_message
- End Property
- End Class
-
- Sub ValidateMinLength(instance, field_name, size, message)
- if not IsObject(instance.Validator) then set instance.Validator = new Validator_Class
- instance.Validator.AddValidation new MinLengthValidation_Class.Initialize(instance, field_name, size, message)
- End Sub
-
- '-----------------------------------------------------------------------------------------------------------------------
- ' Max Length Validation
- '-----------------------------------------------------------------------------------------------------------------------
- Class MaxLengthValidation_Class
- Private m_instance
- Private m_field_name
- Private m_size
- Private m_message
- Private m_ok
-
- Public Function Initialize(instance, field_name, size, message)
- set m_instance = instance
- m_field_name = field_name
- m_size = size
- m_message = message
- m_ok = true
- set Initialize = Me
- End Function
-
- Public Sub Check
- If Len(eval("m_instance." & m_field_name)) > m_size then m_ok = false
- End Sub
-
- Public Property Get OK
- OK = m_ok
- End Property
-
- Public Property Get Message
- Message = m_message
- End Property
- End Class
-
- Sub ValidateMaxLength(instance, field_name, size, message)
- if not IsObject(instance.Validator) then set instance.Validator = new Validator_Class
- instance.Validator.AddValidation new MaxLengthValidation_Class.Initialize(instance, field_name, size, message)
- End Sub
-
- '-----------------------------------------------------------------------------------------------------------------------
- ' Numeric Validation
- '-----------------------------------------------------------------------------------------------------------------------
- Class NumericValidation_Class
- Private m_instance
- Private m_field_name
- Private m_message
- Private m_ok
-
- Public Function Initialize(instance, field_name, message)
- set m_instance = instance
- m_field_name = field_name
- m_message = message
- m_ok = true
- set Initialize = Me
- End Function
-
- Public Sub Check
- If Not IsNumeric(eval("m_instance." & m_field_name)) then m_ok = false
- End Sub
-
- Public Property Get OK
- OK = m_ok
- End Property
-
- Public Property Get Message
- Message = m_message
- End Property
- End Class
-
- Sub ValidateNumeric(instance, field_name, message)
- if not IsObject(instance.Validator) then set instance.Validator = new Validator_Class
- instance.Validator.AddValidation new NumericValidation_Class.Initialize(instance, field_name, message)
- End Sub
-
-
- '-----------------------------------------------------------------------------------------------------------------------
- ' Regular Expression Pattern Validation
- '-----------------------------------------------------------------------------------------------------------------------
- Class PatternValidation_Class
- Private m_instance
- Private m_field_name
- Private m_pattern
- Private m_message
- Private m_ok
-
- Public Function Initialize(instance, field_name, pattern, message)
- set m_instance = instance
- m_field_name = field_name
- m_pattern = pattern
- m_message = message
- m_ok = true
- set Initialize = Me
- End Function
-
- Public Sub Check
- dim re : set re = new RegExp
- With re
- .Pattern = m_pattern
- .Global = true
- .IgnoreCase = true
- End With
- dim matches : set matches = re.Execute(eval("m_instance." & m_field_name))
- if matches.Count = 0 then
- m_ok = false
- end if
- End Sub
-
- Public Property Get OK
- OK = m_ok
- End Property
-
- Public Property Get Message
- Message = m_message
- End Property
- End Class
-
- Sub ValidatePattern(instance, field_name, pattern, message)
- if not IsObject(instance.Validator) then set instance.Validator = new Validator_Class
- instance.Validator.AddValidation new PatternValidation_Class.Initialize(instance, field_name, pattern, message)
- End Sub
-
-
-
- '-----------------------------------------------------------------------------------------------------------------------
- ' Validator Class
- ' This class is not intended to be used directly. Models should use the Validate* subs instead.
- '-----------------------------------------------------------------------------------------------------------------------
- Class Validator_Class
- Private m_validations
- Private m_errors
-
- Private Sub Class_Initialize
- m_validations = Array()
- redim m_validations(-1)
-
- m_errors = Array()
- redim m_errors(-1)
- End Sub
-
- Public Property Get Errors
- Errors = m_errors
- End Property
-
- Public Sub AddValidation(validation)
- dim n : n = ubound(m_validations) + 1
- redim preserve m_validations(n)
- set m_validations(n) = validation
- End Sub
-
- Public Sub Validate
- dim n : n = ubound(m_validations)
- dim i, V
- for i = 0 to n
- set V = m_validations(i)
- V.Check
- if not V.OK then
- AddError V.Message
- end if
- next
- End Sub
-
- Public Property Get HasErrors
- HasErrors = (ubound(m_errors) > -1)
- End Property
-
- 'Public to allow other errors to be added by the controller for circumstances not accounted for by the validators
- Public Sub AddError(msg)
- redim preserve m_errors(ubound(m_errors) + 1)
- m_errors(ubound(m_errors)) = msg
- End Sub
- End Class
- %>
|