/
validation.php
244 lines (213 loc) · 7.87 KB
/
validation.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
/*
* CakePHP jQuery Validation Plugin
* Copyright (c) 2009 Matt Curry
* www.PseudoCoder.com
* http://github.com/mcurry/cakephp_plugin_validation
* http://sandbox2.pseudocoder.com/demo/validation
*
* @author mattc <matt@pseudocoder.com>
* @license MIT
*
*/
//feel free to replace these or overwrite in your bootstrap.php
if (!defined('VALID_EMAIL_JS')) {
define('VALID_EMAIL_JS', '/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/');
}
//I know the octals should be capped at 255
if (!defined('VALID_IP_JS')) {
define('VALID_IP_JS', '/^[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}$/');
}
class ValidationHelper extends Helper {
var $helpers = array('Javascript');
//For security reasons you may not want to include all possible validations.
//In your bootstrap you can define which are allowed
//Configure::write('javascriptValidationWhitelist', array('alphaNumeric', 'minLength'));
var $whitelist = false;
function bind($modelNames, $options=array()) {
$defaultOptions = array('form' => 'form', 'inline' => true, 'messageId' => null);
$options = am($defaultOptions, $options);
$pluginOptions = array_intersect_key($options, array('messageId' => true));
//load the whitelist
$this->whitelist = Configure::read('javascriptValidationWhitelist');
$validation = array();
if (!is_array($modelNames)) {
$modelNames = array($modelNames);
}
//filter the rules to those that can be handled with JavaScript
foreach($modelNames as $modelName) {
$model = classRegistry::init($modelName);
foreach ($model->validate as $field => $validators) {
if (array_intersect(array('rule', 'allowEmpty', 'on', 'message', 'last'), array_keys($validators))) {
$validators = array($validators);
}
foreach($validators as $key => $validator) {
$rule = null;
if (!is_array($validator)) {
$validator = array('rule' => $validator);
}
//skip rules that are applied only on created or updates
if (!empty($validator['on'])) {
continue;
}
if (!isset($validator['message'])) {
$validator['message'] = sprintf('%s %s',
__('There was a problem with the field', true),
Inflector::humanize($field)
);
}
if (!empty($validator['rule'])) {
$rule = $this->__convertRule($validator['rule']);
}
if ($rule) {
$temp = array('rule' => $rule,
'message' => __($validator['message'], true)
);
if (isset($validator['last']) && $validator['last'] === true) {
$temp['last'] = true;
}
if (isset($validator['allowEmpty']) && $validator['allowEmpty'] === true) {
$temp['allowEmpty'] = true;
}
if (in_array($validator['rule'], array('blank'))) {
//Cake Validation::_check returning true is actually false for blank
//add a "!" so that JavaScript knows
$temp['negate'] = true;
}
$validation[$modelName . Inflector::camelize($field)][] = $temp;
}
}
}
}
if ($options['form']) {
$js = sprintf('$(function() { $("%s").validate(%s, %s) });',
$options['form'],
$this->Javascript->object($validation),
$this->Javascript->object($pluginOptions));
} else {
return $this->Javascript->object($validation);
}
if ($options['inline']) {
return sprintf($this->Javascript->tags['javascriptblock'], $js);
} else {
$this->Javascript->codeBlock($js, array('inline' => false));
}
return;
}
function __convertRule($rule) {
$regex = false;
if ($rule == '_extract') {
return false;
}
if (is_array($this->whitelist) && !in_array($rule, $this->whitelist)) {
return false;
}
$params = array();
if (is_array($rule)) {
$params = array_slice($rule, 1);
$rule = $rule[0];
}
if ($rule == 'comparison') {
$params[0] = str_replace(array(' ', "\t", "\n", "\r", "\0", "\x0B"), '', strtolower($params[0]));
switch ($params[0]) {
case 'isgreater':
$params[0] = '>';
break;
case 'isless':
$params[0] = '<';
break;
case 'greaterorequal':
$params[0] = '>=';
break;
case 'lessorequal':
$params[0] = '<=';
break;
case 'equalto':
$params[0] = '==';
break;
case 'notequal':
$params[0] = '!=';
break;
}
}
//Certain Cake built-in validations can be handled with regular expressions,
//but aren't on the Cake side.
switch ($rule) {
case 'alphaNumeric':
return '/^[0-9A-Za-z]+$/';
case 'between':
return sprintf('/^.{%d,%d}$/', $params[0], $params[1]);
case 'boolean':
return array('rule' => 'boolean');
case 'date':
//Some of Cake's date regexs aren't JavaScript compatible. Skip for now
if (!empty($params[0])) {
$params = $params[0];
} else {
$params = 'ymd';
}
return array('rule' => 'date', 'params' => $params);
case 'email':
return VALID_EMAIL_JS;
case 'equalTo':
return sprintf('/^%s$/', $params[0]);
case 'extension':
if (empty($params[0])) {
$params = array('gif', 'jpeg', 'png', 'jpg');
} else {
$params = $params[0];
}
return sprintf('/\.(%s)$/', implode('|', $params));
case 'inList':
return array('rule' => 'inList', 'params' => $params[0]);
case 'ip':
return VALID_IP_JS;
case 'minLength':
return sprintf('/^.{%d,}$/', $params[0]);
case 'maxLength':
return sprintf('/^.{0,%d}$/', $params[0]);
case 'money':
//The Cake regex for money was giving me issues, even within PHP. Skip for now
return array('rule' => 'money');
case 'multiple':
$defaults = array('in' => null, 'max' => null, 'min' => null);
$params = array_merge($defaults, $params[0]);
return array('rule' => 'multiple', 'params' => $params);
case 'numeric':
//Cake uses PHP's is_numeric function, which actually accepts a varied input
//(both +0123.45e6 and 0xFF are valid) then what is allowed in this regular expression.
//99% of people using this validation probably want to restrict to just numbers in standard
//decimal notation. Feel free to alter or delete.
return '/^[+-]?[0-9|.]+$/';
case 'range':
case 'comparison':
//Don't think there is a way to do this with a regular expressions,
//so we'll handle this with plain old javascript
return array('rule' => $rule, 'params' => array($params[0], $params[1]));
}
//try to lookup the regular expression from
//CakePHP's built-in validation rules
$Validation =& Validation::getInstance();
if (method_exists($Validation, $rule)) {
$Validation->regex = null;
call_user_func_array(array(&$Validation, $rule), array_merge(array(true), $params));
if ($Validation->regex) {
$regex = $Validation->regex;
}
}
if ($regex) {
//special handling
switch ($rule) {
case 'postal':
case 'ssn':
//I'm not a regex guru and I have no idea what "\\A\\b" and "\\b\\z" do.
//Is it just to match start and end of line? Why not use
//"^" and "$" then? Eitherway they don't work in JavaScript.
return str_replace(array('\\A\\b', '\\b\\z'), array('^', '$'), $regex);
}
return $regex;
}
return array('rule' => $rule, 'params' => $params);
}
}
?>