-
Notifications
You must be signed in to change notification settings - Fork 1
/
MyValidator.class.php
102 lines (91 loc) · 3.21 KB
/
MyValidator.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/**
*
* Copyright (c) 2003-2012 Ramon Antonio Parada <[email protected]>
*
* Instance validator
*
* @category Database
* @package MyActiveRecord
* @author Ramon Antonio Parada <[email protected]>
* @copyright 2012 Ramon Antonio Parada
* @version 0.5
*/
class MyValidator {
/**
* Validates that an attribute has a value, adding an error
* to the object if the value is empty.
*
* @param string strKey name of field/attribute/key
* @param string strMessage Error message to record if value does not match
* @return boolean True if the field has a value. False if it does not.
*/
function validate_existence($strKey, $strMessage=null) {
if ( !empty($this->$strKey) ) {
return true;
} else {
$this->add_error($strKey, $strMessage ? $strMessage : 'Missing '.$strKey);
return false;
}
}
/**
* Validates the value of an attribute against a regular
* expression, adding an error to the object if the value
* does not match.
*
* @param string strKey name of field/attribute/key
* @param string strRegExp Regular Expression
* @param string strMessage Error message to record if value does not match
* @return boolean True if the field matches. False if it does not match.
*/
function validate_regexp($strKey, $strRegExp, $strMessage=null) {
if( preg_match($strRegExp, $this->$strKey) ) {
return true;
} else {
$this->add_error($strKey, $strMessage ? $strMessage : 'Invalid '.$strKey);
return false;
}
}
/**
* Validates the uniqueness of the value of a given field/key.
* Adds error to object if field is not unique
*
* @param string strKey name of field/attribute/key
* @param string strMessage Error message to record if value is not unique
* @return boolean true if field is unique, false if not
*/
function validate_uniqueness_of($strKey, $strMessage=null) {
if ( empty($this->id) && MyActiveRecord::Count( get_class($this), "$strKey = '{$this->$strKey}'" ) > 0 ) {
$this->add_error($strKey, $strMessage ? $strMessage : ucfirst($strKey).' is not unique');
return false;
} elseif(MyActiveRecord::Count( get_class($this), "$strKey = '{$this->$strKey}' AND id != " . ($this->id + 0) ) > 0){
$this->add_error($strKey, $strMessage ? $strMessage : ucfirst($strKey).' is not unique');
return false;
} else {
return true;
}
}
/**
* Checks to see if an e-mail exists, looks like an e-mail, and is unique
*
* @param string $strKey
* @return string
* @author Walter Lee Davis
*/
function validate_unique_email($strKey) {
return $this->validate_existence($strKey,'Please enter your e-mail address') &&
$this->validate_regexp($strKey,"/^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,6}$/i", 'That didn’t look like an e-mail address') &&
$this->validate_uniqueness_of($strKey,'This e-mail address is already registered');
}
/**
* Checks to see if an e-mail exists and looks like an e-mail
*
* @param string $strKey
* @return string
* @author Walter Lee Davis
*/
function validate_email($strKey) {
return $this->validate_existence($strKey,'Please enter your e-mail address') &&
$this->validate_regexp($strKey,"/^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,6}$/i", 'That didn’t look like an e-mail address');
}
}