-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathValidator.module
160 lines (141 loc) · 3.41 KB
/
Validator.module
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/**
* Validator
*
* Provides a set of useful validation methods
*
* ProcessWire 2.x
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* @author Tabea David <[email protected]>
* @version 0.0.3
* @copyright Copyright (c) 2015 KF Interactive, www.kf-interactive.com, <[email protected]>
* @see https://github.com/justonestep/processwire-validator
* @see http://www.processwire.com
*
*/
/**
* Class Validator
*/
class Validator extends WireData implements Module {
/**
* getModuleInfo
*
* @return array
*/
public static function getModuleInfo() {
return array(
'title' => 'Validator',
'summary' => 'Provides a set of useful validation methods',
'version' => 3,
'author' => 'Tabea David | [email protected]',
'href' => 'https://github.com/justonestep/processwire-validator',
'icon' => 'check-circle-o',
'singular' => false,
'autoload' => true
);
}
/**
* array available validators
*/
protected static $validators = array(
'ContainsAtLeast',
'IsEmail',
'IsEmpty',
'IsEqual',
'IsEqualLength',
'IsUnique',
'MaxLength',
'MinLength',
'NoWhitespace',
'Range',
);
protected $errors = array();
protected $messages = array();
/**
* construct - require validators
*/
public function __construct() {
require_once(wire('config')->paths->Validator . 'lib/AbstractValidator.php');
require_once(wire('config')->paths->Validator . 'lib/ValidatorInterface.php');
foreach (self::$validators as $validator) {
require_once(wire('config')->paths->Validator . 'lib/' . $validator . 'Validator.php');
}
}
/**
* Execute validation
*
* @param array $conf
* @return array
*/
protected function validate($conf) {
foreach ($conf as $ident => $validators) {
foreach ($validators as $validator => $options) {
if (!is_array($options)) {
$validator = $options;
$options = array();
}
$classname = 'Kfi\Validator\\' . ucfirst($validator) . 'Validator';
$result = new $classname(wire('input')->post->$ident, $options);
if (!$result->isValid()) {
$this->errors[$ident][] = $result->getErrors();
$this->messages[$ident][] = $result->getMessages();
if (in_array($validator, array('isEmpty', 'isEmail'))) break;
}
}
}
}
/**
* set configuration array
*/
public function setConfig($conf) {
if (is_array($conf)) {
$this->validate($conf);
}
}
/**
* get validation errors
*
* @return array
*/
public function getErrors() {
return $this->array_multi_merge($this->errors);
}
/**
* get validation messages
*
* @return array
*/
public function getMessages() {
return $this->array_multi_merge($this->messages);
}
/**
* get validation state
*
* @return boolean
*/
public function isValid() {
return count($this->errors) ? false : true;
}
/**
* Merge a multidimensional array
*
* @param array $array
* @return array
*/
protected function array_multi_merge($array) {
$result = array();
if (count($array)) {
foreach ($array as $key => $value) {
$new = array();
foreach ($value as $items) {
foreach ($items as $item) {
$new[] = $item;
}
}
$result[$key] = $new;
}
}
return $result;
}
}