-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathValidate.php
More file actions
70 lines (59 loc) · 1.52 KB
/
Copy pathValidate.php
File metadata and controls
70 lines (59 loc) · 1.52 KB
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
<?php
class Validate {
private $_passed = false,
$_errors = array(),
$_db = null;
public function __construct(){
$this->_db = DB::getInstance();
}
public function check($source, $items = array()){
foreach($items as $item => $rules){
foreach($rules as $rule => $rule_value){
$value = $source[$item];
$item = escape($item);
// echo $value;
// echo "{$item} {$rule} must be {$rule_value}<br>"; // display all rules
if($rule === 'required' && empty ($value)){
$this->addError("{$item} is required");
} else if(!empty($value)){
switch ($rule){
case 'min':
if(strlen($value)<$rule_value){
$this->addError("{$item} must be a minimum of {$rule_value} characters.");
}
break;
case 'max':
if(strlen($value)>$rule_value){
$this->addError("{$item} must be a maximum of {$rule_value} characters.");
}
break;
case 'matches':
if($value != $source[$rule_value]){
$this->addError("{$rule_value} must match {$item}");
}
break;
case 'unique':
$check = $this->_db->get($rule_value, array($item,'=',$value));
if ($check->count()){
$this->addError("This {$item} already exists.");
}
break;
}
}
}
}
if(empty($this->_errors)){
$this->_passed = true;
}
return $this;
}
private function addError($error){
$this->_errors[]= $error;
}
public function errors(){
return $this->_errors;
}
public function passed(){
return $this->_passed;
}
}