1
|
|
|
<?php |
2
|
|
|
require_once(dirname(__FILE__) . '/../fwolflib.php'); |
3
|
|
|
require_once(FWOLFLIB . 'func/string.php'); |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Validator, mostly for form submition. |
8
|
|
|
* |
9
|
|
|
* Include both web frontend and php backend check. |
10
|
|
|
* Using jQuery to operate javascript. |
11
|
|
|
* |
12
|
|
|
* Requirement: |
13
|
|
|
* - jQuery |
14
|
|
|
* |
15
|
|
|
* Ref: http://code.google.com/p/easyvalidator/ |
16
|
|
|
* |
17
|
|
|
* @deprecated Use Fwlib\Html\FormValidator |
18
|
|
|
* @package fwolflib |
19
|
|
|
* @subpackage class |
20
|
|
|
* @copyright Copyright © 2011-2013, Fwolf |
21
|
|
|
* @author Fwolf <[email protected]> |
22
|
|
|
* @since 2011-07-21 |
23
|
|
|
*/ |
24
|
|
|
class Validator extends Fwolflib { |
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Validator rule |
28
|
|
|
* |
29
|
|
|
* array( |
30
|
|
|
* id => array( |
31
|
|
|
* id, // String |
32
|
|
|
* rule, // Array of rules str, start with regex, url etc. |
33
|
|
|
* tip, // String |
34
|
|
|
* show-error-blur, // Boolean |
35
|
|
|
* show-error-keyup, // Boolean |
36
|
|
|
* ) |
37
|
|
|
* ) |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
public $aRule = array(); |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* constructor |
45
|
|
|
* |
46
|
|
|
* @param array $ar_cfg |
47
|
|
|
*/ |
48
|
|
|
public function __construct ($ar_cfg = array()) { |
49
|
|
|
parent::__construct($ar_cfg); |
50
|
|
|
} // end of func __construct |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get css for tip... etc |
55
|
|
|
* |
56
|
|
|
* @param boolean $b_with_tag With <script> tag ? |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function GetCss ($b_with_tag = true) { |
60
|
|
|
$s_css = ''; |
61
|
|
|
if ($b_with_tag) |
62
|
|
|
$s_css .= '<style type="text/css" media="screen, print"> |
63
|
|
|
<!-- |
64
|
|
|
'; |
65
|
|
|
|
66
|
|
|
// Css body |
67
|
|
|
$s_css .= ' |
68
|
|
|
div#' . $this->aCfg['id-prefix'] . 'tip { |
69
|
|
|
-moz-border-radius: 5px; |
70
|
|
|
-webkit-border-radius: 5px; |
71
|
|
|
background-color: #F5EBB3; |
72
|
|
|
border: 1px solid #A6C9E2; |
73
|
|
|
position: absolute; |
74
|
|
|
padding: 10px; |
75
|
|
|
left: 5px; |
76
|
|
|
z-index: 999; |
77
|
|
|
} |
78
|
|
|
div#' . $this->aCfg['id-prefix'] . 'tip #' |
79
|
|
|
. $this->aCfg['id-prefix'] . 'tip_arrow { |
80
|
|
|
position: absolute; |
81
|
|
|
top: 38px; |
82
|
|
|
left: 5px |
83
|
|
|
} |
84
|
|
|
.' . $this->aCfg['id-prefix'] . 'fail { |
85
|
|
|
background-color: #F6CBCB; |
86
|
|
|
/* border: 2px solid red;*/ |
87
|
|
|
color: black; |
88
|
|
|
} |
89
|
|
|
' . $this->aCfg['css-add']; |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
if ($b_with_tag) |
93
|
|
|
$s_css .= '--> |
94
|
|
|
</style> |
95
|
|
|
'; |
96
|
|
|
|
97
|
|
|
return $s_css; |
98
|
|
|
} // end of func GetCss |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get validate js |
103
|
|
|
* |
104
|
|
|
* @param boolean $b_with_tag With <script> tag ? |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function GetJs ($b_with_tag = true) { |
108
|
|
|
$s_js = ''; |
109
|
|
|
if ($b_with_tag) |
110
|
|
|
$s_js .= '<script type=\'text/javascript\'> |
111
|
|
|
<!--//--><![CDATA[//> |
112
|
|
|
<!-- |
113
|
|
|
'; |
114
|
|
|
|
115
|
|
|
$s_js .=' |
116
|
|
|
/* Append css define to <head> */ |
117
|
|
|
$(\'head\').append(\'\ |
118
|
|
|
' . str_replace("\n", "\\\n", $this->GetCss()) . '\ |
119
|
|
|
\'); |
120
|
|
|
'; |
121
|
|
|
|
122
|
|
|
if (!empty($this->aRule)) |
123
|
|
|
foreach ($this->aRule as $rule) { |
124
|
|
|
$s_js .= '/* Set validate for ' . $rule['id'] . " */\n"; |
125
|
|
|
$s_js .= $this->GetJsRule($rule); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if (!empty($this->aCfg['form-selector'])) |
129
|
|
|
$s_js .= $this->GetJsFormSubmit( |
130
|
|
|
$this->aCfg['form-selector']); |
131
|
|
|
|
132
|
|
|
if ($b_with_tag) |
133
|
|
|
$s_js .= '//--><!]]> |
134
|
|
|
</script> |
135
|
|
|
'; |
136
|
|
|
return $s_js; |
137
|
|
|
} // end of func GetJs |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get check js for form submit |
142
|
|
|
* |
143
|
|
|
* @param string $s_form Form selector |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
public function GetJsFormSubmit ($s_form) { |
147
|
|
|
$s_js = ''; |
148
|
|
|
|
149
|
|
|
// Need pre define msg alert func ? |
150
|
|
|
if ('jsalert' == $this->aCfg['func-show-error']) |
151
|
|
|
$s_js .= $this->GetJsJsAlert(); |
152
|
|
|
|
153
|
|
|
$s_js .= ' |
154
|
|
|
$(\'' . $s_form . '\').submit(function () { |
155
|
|
|
var ar_err = new Array(); |
156
|
|
|
'; |
157
|
|
|
|
158
|
|
|
if (!empty($this->aRule)) |
159
|
|
|
foreach ($this->aRule as $rule) { |
160
|
|
|
$s_js .= ' |
161
|
|
|
ar_err = ar_err.concat(' |
162
|
|
|
. StrUnderline2Ucfirst($this->aCfg['id-prefix'] |
|
|
|
|
163
|
|
|
. $rule['id'], true) |
164
|
|
|
. '(false)); |
165
|
|
|
'; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$s_js .= ' |
169
|
|
|
/* Show error msg */ |
170
|
|
|
' . $this->GetJsShowErr(); |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
// Experiment focus validate fail input after alert error. |
174
|
|
|
// This can't attach to blur event of each input, |
175
|
|
|
// that may call alert recurrently, which is infinite loop. |
176
|
|
|
// So use it only when form submit. |
177
|
|
|
if (true == $this->aCfg['show-error-focus']) |
178
|
|
|
$s_js .= ' |
179
|
|
|
var f = function() { |
180
|
|
|
$(\'.' . $this->aCfg['id-prefix'] . 'fail\') |
181
|
|
|
.first().focus(); |
182
|
|
|
$(\'' . $s_form . '\').unbind(\'mouseover\', f); |
183
|
|
|
}; |
184
|
|
|
$(\'' . $s_form . '\').bind(\'mouseover\', f); |
185
|
|
|
'; |
186
|
|
|
|
187
|
|
|
|
188
|
|
|
// Disable submit botton |
189
|
|
|
if (!empty($this->aCfg['form-submit-delay'])) |
190
|
|
|
$s_js .= ' |
191
|
|
|
/* Disable multi submit for some time */ |
192
|
|
|
$(\'' . $s_form . ' input[type="submit"]\') |
193
|
|
|
.attr(\'disabled\', true); |
194
|
|
|
setTimeout(function () { |
195
|
|
|
$(\'' . $s_form . ' input[type="submit"]\') |
196
|
|
|
.removeAttr(\'disabled\'); |
197
|
|
|
}, ' |
198
|
|
|
. (1000 * $this->aCfg['form-submit-delay']) |
199
|
|
|
. '); |
200
|
|
|
'; |
201
|
|
|
|
202
|
|
|
|
203
|
|
|
$s_js .= ' |
204
|
|
|
return (0 == ar_err.length); |
205
|
|
|
}); |
206
|
|
|
'; |
207
|
|
|
|
208
|
|
|
return $s_js; |
209
|
|
|
} // end of func GetJsFormSubmit |
210
|
|
|
|
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Get js for display alert msg using float div |
214
|
|
|
* |
215
|
|
|
* @return string |
216
|
|
|
*/ |
217
|
|
|
public function GetJsJsAlert () { |
218
|
|
|
$s_js = ''; |
219
|
|
|
$s_func = StrUnderline2Ucfirst($this->aCfg['id-prefix'] |
|
|
|
|
220
|
|
|
, true) . 'JsAlert'; |
221
|
|
|
|
222
|
|
|
$s_id_div = $this->aCfg['id-prefix'] . 'js_alert'; |
223
|
|
|
$s_id_div_bg = $this->aCfg['id-prefix'] . 'js_alert_bg'; |
224
|
|
|
foreach (array('div', 'bg', 'fieldset', 'legend', 'li') |
225
|
|
|
as $k) { |
226
|
|
|
if (!empty($this->aCfg['func-jsalert-css-' . $k])) |
227
|
|
|
$s = str_replace("\n", "\\\n" |
|
|
|
|
228
|
|
|
, $this->aCfg['func-jsalert-css-' . $k]); |
229
|
|
|
eval('$s_css_' . $k . ' = $s;'); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
$s_js .= ' |
233
|
|
|
function ' . $s_func . ' (msg) { |
234
|
|
|
s_msg = \'\'; |
235
|
|
|
$(msg).each(function () { |
236
|
|
|
s_msg += \'<li>\' + this + \'</li>\'; |
237
|
|
|
}); |
238
|
|
|
|
239
|
|
|
/* Set css */ |
240
|
|
|
if (\'undefined\' == typeof(b_' . $this->aCfg['id-prefix'] |
241
|
|
|
. 'css_setted)) { |
242
|
|
|
var b_' . $this->aCfg['id-prefix'] . 'css_setted = 1; |
243
|
|
|
$(\'head\').append(\'\ |
244
|
|
|
<style type="text/css" media="screen, print">\ |
245
|
|
|
<!--\ |
246
|
|
|
#' . $s_id_div . ' {\ |
247
|
|
|
' . $s_css_div . '\ |
|
|
|
|
248
|
|
|
}\ |
249
|
|
|
#' . $s_id_div_bg . ' {\ |
250
|
|
|
' . $s_css_bg . '\ |
|
|
|
|
251
|
|
|
}\ |
252
|
|
|
#' . $s_id_div . ' fieldset {\ |
253
|
|
|
' . $s_css_fieldset . '\ |
|
|
|
|
254
|
|
|
}\ |
255
|
|
|
#' . $s_id_div . ' legend {\ |
256
|
|
|
' . $s_css_legend . '\ |
|
|
|
|
257
|
|
|
}\ |
258
|
|
|
#' . $s_id_div . ' li {\ |
259
|
|
|
' . $s_css_li . '\ |
|
|
|
|
260
|
|
|
}\ |
261
|
|
|
-->\ |
262
|
|
|
</style>\ |
263
|
|
|
\'); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
$(\'body\').append(\'\ |
267
|
|
|
<div id="' . $s_id_div . '" class="' |
268
|
|
|
. $this->aCfg['func-jsalert-class'] . '">\ |
269
|
|
|
<fieldset>\ |
270
|
|
|
<legend align="center"> ' |
271
|
|
|
. $this->aCfg['func-jsalert-legend'] |
272
|
|
|
. ' </legend>\ |
273
|
|
|
<ul>\ |
274
|
|
|
\' + s_msg + \'\ |
275
|
|
|
<li><a href="javascript:void(0);"\ |
276
|
|
|
onclick="$(\\\'#' . $s_id_div |
277
|
|
|
. '\\\').remove();' |
278
|
|
|
. '$(\\\'#' . $s_id_div_bg |
279
|
|
|
. '\\\').remove();">' |
280
|
|
|
. $this->aCfg['func-jsalert-close'] |
281
|
|
|
. '</a></li>\ |
282
|
|
|
</ul>\ |
283
|
|
|
</fieldset>\ |
284
|
|
|
</div>\ |
285
|
|
|
<div id="' . $s_id_div_bg . '"></div>\ |
286
|
|
|
\'); |
287
|
|
|
|
288
|
|
|
$(\'#' . $s_id_div . '\').css(\'top\' |
289
|
|
|
, ((window.innerHeight |
290
|
|
|
|| document.documentElement.offsetHeight) |
291
|
|
|
- $(\'#' . $s_id_div . ' fieldset\').height()) |
292
|
|
|
/ 2 |
293
|
|
|
+ (document.body.scrollTop |
294
|
|
|
|| document.documentElement.scrollTop) |
295
|
|
|
+ \'px\'); |
296
|
|
|
$(\'#' . $s_id_div_bg . '\') |
297
|
|
|
.height($(document).height() * 1.2); |
298
|
|
|
} /* end of func ' . $s_func . ' */ |
299
|
|
|
'; |
300
|
|
|
|
301
|
|
|
return $s_js; |
302
|
|
|
} // end of func GetJsJsAlert |
303
|
|
|
|
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Get validate js of one rule |
307
|
|
|
* |
308
|
|
|
* @param array $ar_rule Id, tip/text: string; |
309
|
|
|
* rule: string/array of string. |
310
|
|
|
* @return string |
311
|
|
|
*/ |
312
|
|
|
public function GetJsRule ($ar_rule) { |
313
|
|
|
$s_js = ''; |
314
|
|
|
if (!empty($ar_rule['tip'])) |
315
|
|
|
$s_js .= $this->GetJsTip($ar_rule['id'], $ar_rule['tip']); |
316
|
|
|
|
317
|
|
|
$s_func = StrUnderline2Ucfirst($this->aCfg['id-prefix'] |
|
|
|
|
318
|
|
|
. $ar_rule['id'], true); |
319
|
|
|
// Validate func for this control |
320
|
|
|
$s_js .= ' |
321
|
|
|
/** |
322
|
|
|
* Validate ' . $ar_rule['id'] . ' |
323
|
|
|
* |
324
|
|
|
* @param boolean b_alert_err Alert when got err |
325
|
|
|
* @return array Empty means no error. |
326
|
|
|
*/ |
327
|
|
|
function ' . $s_func . ' (b_alert_err) { |
328
|
|
|
var obj = $(\'#' . $ar_rule['id'] . '\'); |
329
|
|
|
var ar_err = Array(); |
330
|
|
|
/* Standard error, rule str can customize it. */ |
331
|
|
|
var s_err = \'' . $ar_rule['tip'] . '\'; |
332
|
|
|
'; |
333
|
|
|
|
334
|
|
|
// Input is disabled ? |
335
|
|
|
if (false == $this->aCfg['check-disabled']) |
336
|
|
|
$s_js .= ' |
337
|
|
|
if (true == obj.attr(\'disabled\')) |
338
|
|
|
return ar_err; |
339
|
|
|
'; |
340
|
|
|
|
341
|
|
|
// Show loading img ? part 1/2 |
342
|
|
|
if (!empty($this->aCfg['path-img-loading'])) |
343
|
|
|
$s_js .= ' |
344
|
|
|
obj.after(\'<img id="' |
345
|
|
|
. $this->aCfg['id-prefix'] . 'loading" src="' |
346
|
|
|
. $this->aCfg['path-img-loading'] . '" />\'); |
347
|
|
|
'; |
348
|
|
|
|
349
|
|
|
$s_js .= ' |
350
|
|
|
/* Do check */ |
351
|
|
|
' . $this->GetJsRuleStr($ar_rule) . ' |
352
|
|
|
if (0 < ar_err.length) { |
353
|
|
|
obj.addClass(\'' |
354
|
|
|
. $this->aCfg['id-prefix'] . 'fail\'); |
355
|
|
|
} |
356
|
|
|
else { |
357
|
|
|
obj.removeClass(\'' |
358
|
|
|
. $this->aCfg['id-prefix'] . 'fail\'); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/* If err msg not start with label, prepend it. */ |
362
|
|
|
var s_label = \'\'; |
363
|
|
|
if (0 < ar_err.length) { |
364
|
|
|
$(ar_err).each(function (index, value) { |
365
|
|
|
s_label = $.trim($(\'label[for="' . $ar_rule['id'] |
366
|
|
|
. '"]\').text()) |
367
|
|
|
.replace(/(:|:)/g, \'\'); |
368
|
|
|
if ((0 < s_label.length) && |
369
|
|
|
(null == value.match(\'^\' + s_label))) |
370
|
|
|
ar_err[index] = (s_label + \'' |
371
|
|
|
. $this->aCfg['tip-separator'] . '\' |
372
|
|
|
+ value); |
373
|
|
|
}); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/* Alert err ? default false. */ |
377
|
|
|
if (\'undefined\' == typeof(b_alert_err)) |
378
|
|
|
b_alert_err = false; |
379
|
|
|
if (true == b_alert_err) { |
380
|
|
|
' . $this->GetJsShowErr() . ' |
381
|
|
|
} |
382
|
|
|
'; |
383
|
|
|
|
384
|
|
|
// Show loading img ? part 2/2 |
385
|
|
|
if (!empty($this->aCfg['path-img-loading'])) |
386
|
|
|
$s_js .= ' |
387
|
|
|
$(\'#' . $this->aCfg['id-prefix'] . 'loading\') |
388
|
|
|
.remove(); |
389
|
|
|
'; |
390
|
|
|
|
391
|
|
|
$s_js .= ' |
392
|
|
|
return ar_err; |
393
|
|
|
} /* end of func ' . $s_func . ' */ |
394
|
|
|
'; |
395
|
|
|
|
396
|
|
|
// Do validate when blur, and alert if setted |
397
|
|
|
$s_js .= ' |
398
|
|
|
$(\'#' . $ar_rule['id'] . '\').blur(function() { |
399
|
|
|
'; |
400
|
|
|
if (true == (isset($ar_rule['show-error-blur']) |
401
|
|
|
? $ar_rule['show-error-blur'] |
402
|
|
|
: $this->aCfg['show-error-blur'])) |
403
|
|
|
$s_js .= $s_func . '(true);'; |
404
|
|
|
else |
405
|
|
|
$s_js .= $s_func . '(false);'; |
406
|
|
|
$s_js .= ' |
407
|
|
|
}); |
408
|
|
|
'; |
409
|
|
|
|
410
|
|
|
// Do validate when keyup, but no alert |
411
|
|
|
if (true == (isset($ar_rule['show-error-keyup']) |
412
|
|
|
? $ar_rule['show-error-keyup'] |
413
|
|
|
: $this->aCfg['show-error-keyup'])) { |
414
|
|
|
$s_js .= ' |
415
|
|
|
$(\'#' . $ar_rule['id'] . '\').keyup(function() { |
416
|
|
|
' . $s_func . '(false); |
417
|
|
|
}); |
418
|
|
|
'; |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
return $s_js; |
422
|
|
|
} // end of func GetJsRule |
423
|
|
|
|
424
|
|
|
|
425
|
|
|
/** |
426
|
|
|
* Get js check str for rule(s) |
427
|
|
|
* |
428
|
|
|
* @param array $ar_rule Id, tip/text: string; |
429
|
|
|
* rule: string/array of string. |
430
|
|
|
* @return string |
431
|
|
|
*/ |
432
|
|
|
public function GetJsRuleStr ($ar_rule) { |
433
|
|
|
if (!is_array($ar_rule['rule'])) |
434
|
|
|
$ar_rule['rule'] = array($ar_rule['rule']); |
435
|
|
|
|
436
|
|
|
$s_js = ''; |
437
|
|
|
foreach ($ar_rule['rule'] as $rule) { |
438
|
|
|
// Call by rule cat |
439
|
|
|
if ('required' == substr($rule, 0, 8)) |
440
|
|
|
$s_js .= $this->GetJsRuleStrRequired(); |
441
|
|
|
elseif ('regex:' == substr($rule, 0, 6)) |
442
|
|
|
$s_js .= $this->GetJsRuleStrRegex(substr($rule, 6)); |
443
|
|
|
elseif ('url:' == substr($rule, 0, 4)) |
444
|
|
|
$s_js .= $this->GetJsRuleStrUrl(substr($rule, 4)); |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
return $s_js; |
448
|
|
|
} // end of func GetjsRuleStr |
449
|
|
|
|
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* Get js for rule: regex |
453
|
|
|
* |
454
|
|
|
* @param string $rule |
455
|
|
|
* @return string |
456
|
|
|
*/ |
457
|
|
|
public function GetJsRuleStrRegex ($rule) { |
458
|
|
|
$rule = trim($rule); |
459
|
|
|
$s_js = ' |
460
|
|
|
var reg_validate = new RegExp(' . $rule . '); |
461
|
|
|
if (!reg_validate.test($.trim(obj.val()))) |
462
|
|
|
ar_err.push(s_err); |
463
|
|
|
'; |
464
|
|
|
return $s_js; |
465
|
|
|
} // end of func GetJsRuleStrRegex |
466
|
|
|
|
467
|
|
|
|
468
|
|
|
/** |
469
|
|
|
* Get js for rule: required |
470
|
|
|
* |
471
|
|
|
* @return string |
472
|
|
|
*/ |
473
|
|
|
public function GetJsRuleStrRequired () { |
474
|
|
|
$s_js = ' |
475
|
|
|
if (0 == $.trim(obj.val()).length) |
476
|
|
|
ar_err.push(s_err); |
477
|
|
|
'; |
478
|
|
|
return $s_js; |
479
|
|
|
} // end of func GetJsRuleStrRequired |
480
|
|
|
|
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* Get js for rule: url |
484
|
|
|
* |
485
|
|
|
* Rule: url [, [id] | [id in form=id in url]] |
486
|
|
|
* |
487
|
|
|
* @param string $rule |
488
|
|
|
* @return string |
489
|
|
|
*/ |
490
|
|
|
public function GetJsRuleStrUrl ($rule) { |
491
|
|
|
$rule = trim($rule); |
492
|
|
|
$ar = explode(',', $rule); |
493
|
|
|
|
494
|
|
|
$s_js = ' |
495
|
|
|
var s_id = obj.attr(\'id\'); |
496
|
|
|
/* |
497
|
|
|
Object can use as map type later in ajax post, |
498
|
|
|
while array cannot. |
499
|
|
|
*/ |
500
|
|
|
var data = new Object; |
501
|
|
|
|
502
|
|
|
/* Gen post data */ |
503
|
|
|
'; |
504
|
|
|
|
505
|
|
|
// Prepare data to do ajax post |
506
|
|
|
if (0 == count($ar)) |
507
|
|
|
// No id assigned, data is itself only |
508
|
|
|
$s_js .= ' |
509
|
|
|
data[s_id] = obj.val(); |
510
|
|
|
data = $.param(data); |
511
|
|
|
'; |
512
|
|
|
elseif ('*' == trim($ar[1])) |
513
|
|
|
// All form content needed |
514
|
|
|
$s_js .= ' |
515
|
|
|
data = obj.parent(\'form\').serialize(); |
516
|
|
|
'; |
517
|
|
|
else { |
518
|
|
|
// Itself and some other input needed, and may have rename |
519
|
|
|
|
520
|
|
|
// Itself, useful here if later rules not include it self. |
521
|
|
|
$s_js .= ' |
522
|
|
|
data[s_id] = obj.val(); |
523
|
|
|
'; |
524
|
|
|
for ($i = 1; $i < count($ar); $i++) { |
|
|
|
|
525
|
|
|
$ar[$i] = trim($ar[$i]); |
526
|
|
|
if (empty($ar[$i])) |
527
|
|
|
continue; |
528
|
|
|
|
529
|
|
|
// Single val or indexed ? |
530
|
|
|
if (0 < strpos($ar[$i], '=')) { |
531
|
|
|
// Indexed |
532
|
|
|
list($s_id_form, $s_id_url) = explode('=', $ar[$i]); |
533
|
|
|
$s_js .= ' |
534
|
|
|
data[\'' . trim($s_id_url) . '\'] = $(\'#' |
535
|
|
|
. trim($s_id_form) . '\').val(); |
536
|
|
|
'; |
537
|
|
|
} |
538
|
|
|
else |
539
|
|
|
// Single val |
540
|
|
|
$s_js .= ' |
541
|
|
|
data[\'' . $ar[$i] . '\'] = $(\'#' |
542
|
|
|
. $ar[$i] . '\').val(); |
543
|
|
|
'; |
544
|
|
|
} |
545
|
|
|
$s_js .= ' |
546
|
|
|
data = $.param(data); |
547
|
|
|
'; |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
$s_js .= ' |
551
|
|
|
$.ajax({ |
552
|
|
|
async: false, |
553
|
|
|
url: \'' . trim($ar[0]) . '\', |
554
|
|
|
data: data, |
555
|
|
|
dataType: \'json\', |
556
|
|
|
type: \'POST\', |
557
|
|
|
success: function(msg) { |
558
|
|
|
/* Json return object, need convert to array */ |
559
|
|
|
if (0 < msg.length) |
560
|
|
|
ar_err = ar_err.concat($.makeArray(msg)); |
561
|
|
|
}, |
562
|
|
|
error: function (jqXHR, textStatus, errorThrown) { |
563
|
|
|
ar_err.push(\'Ajax request error code \' |
564
|
|
|
+ jqXHR.status + \': \' + jqXHR.responseText); |
565
|
|
|
} |
566
|
|
|
}); |
567
|
|
|
'; |
568
|
|
|
return $s_js; |
569
|
|
|
} // end of func GetJsRuleStrUrl |
570
|
|
|
|
571
|
|
|
|
572
|
|
|
/** |
573
|
|
|
* Get js for show error msg by setting |
574
|
|
|
* |
575
|
|
|
* Array contain err msg is named 'ar_err'. |
576
|
|
|
* @return string |
577
|
|
|
*/ |
578
|
|
|
public function GetJsShowErr () { |
579
|
|
|
$s_js = ''; |
580
|
|
|
// Error alert part. |
581
|
|
|
if (!empty($this->aCfg['func-show-error'])) { |
582
|
|
|
if ('alert' == $this->aCfg['func-show-error']) |
583
|
|
|
$s_js .= ' |
584
|
|
|
if (0 < ar_err.length) |
585
|
|
|
alert(ar_err.join("\n")); |
586
|
|
|
'; |
587
|
|
|
elseif ('jsalert' == $this->aCfg['func-show-error']) |
588
|
|
|
$s_js .= ' |
589
|
|
|
if (0 < ar_err.length) |
590
|
|
|
' . StrUnderline2Ucfirst($this->aCfg['id-prefix'] |
|
|
|
|
591
|
|
|
, true) . 'JsAlert(ar_err); |
592
|
|
|
'; |
593
|
|
|
elseif ('JsAlert' == $this->aCfg['func-show-error']) |
594
|
|
|
$s_js .= ' |
595
|
|
|
if (0 < ar_err.length) |
596
|
|
|
JsAlert(ar_err, \'\', \'t' . mt_rand(0, 9000) . '\'); |
597
|
|
|
'; |
598
|
|
|
} |
599
|
|
|
return $s_js; |
600
|
|
|
} // end of func GetJsShowErr |
601
|
|
|
|
602
|
|
|
|
603
|
|
|
/** |
604
|
|
|
* Get validate tip |
605
|
|
|
* |
606
|
|
|
* @param string $id |
607
|
|
|
* @param string $tip |
608
|
|
|
* @return string |
609
|
|
|
*/ |
610
|
|
|
public function GetJsTip ($id, $tip) { |
611
|
|
|
if (empty($tip)) |
612
|
|
|
return ''; |
613
|
|
|
|
614
|
|
|
$s_js = ' |
615
|
|
|
$(\'#' . $id . '\').hover( |
616
|
|
|
function(e) { |
617
|
|
|
$(\'body\').append(\'\ |
618
|
|
|
<div id="' . $this->aCfg['id-prefix'] . 'tip">\ |
619
|
|
|
<img id="' . $this->aCfg['id-prefix'] |
620
|
|
|
. 'tip_arrow"\ |
621
|
|
|
src="' . $this->aCfg['path-img-arrow'] |
622
|
|
|
. '" />\ |
623
|
|
|
' . $tip . '</div>\'); |
624
|
|
|
$(\'div#' . $this->aCfg['id-prefix'] . 'tip\') |
625
|
|
|
.css(\'top\', (e.pageY + ' . |
626
|
|
|
$this->aCfg['tip-offset-y'] . ') + \'px\') |
627
|
|
|
.css(\'left\', (e.pageX + ' . |
628
|
|
|
$this->aCfg['tip-offset-x'] . ') + \'px\'); |
629
|
|
|
}, |
630
|
|
|
function() { |
631
|
|
|
$(\'div#' . $this->aCfg['id-prefix'] . 'tip\') |
632
|
|
|
.remove(); |
633
|
|
|
} |
634
|
|
|
).mousemove( |
635
|
|
|
function(e) { |
636
|
|
|
/* Same with above */ |
637
|
|
|
$(\'div#' . $this->aCfg['id-prefix'] . 'tip\') |
638
|
|
|
.css(\'top\', (e.pageY + ' . |
639
|
|
|
$this->aCfg['tip-offset-y'] . ') + \'px\') |
640
|
|
|
.css(\'left\', (e.pageX + ' . |
641
|
|
|
$this->aCfg['tip-offset-x'] . ') + \'px\'); |
642
|
|
|
} |
643
|
|
|
); |
644
|
|
|
'; |
645
|
|
|
|
646
|
|
|
// Append hint, common known as '*' after input |
647
|
|
|
if (!empty($this->aCfg['hint-text'])) |
648
|
|
|
$s_js .= ' |
649
|
|
|
$(\'#' . $id . '\').after(\'<span class="' |
650
|
|
|
. $this->aCfg['hint-class'] |
651
|
|
|
. '">' . $this->aCfg['hint-text'] . '</span>\ |
652
|
|
|
\'); |
653
|
|
|
'; |
654
|
|
|
|
655
|
|
|
return $s_js; |
656
|
|
|
} // end of func GetJsTip |
657
|
|
|
|
658
|
|
|
|
659
|
|
|
/** |
660
|
|
|
* Reset rules, or some part of it. |
661
|
|
|
* |
662
|
|
|
* @param mixed $id Str or array of str, empty means all. |
663
|
|
|
* @param mixed $part Empty means all part, or assigned. |
664
|
|
|
* @return this |
665
|
|
|
*/ |
666
|
|
|
public function ResetRule ($id = array(), $part = '') { |
667
|
|
|
if (empty($id)) |
668
|
|
|
$this->aRule = array(); |
669
|
|
|
else { |
670
|
|
|
if (!is_array($id)) |
671
|
|
|
$id = array($id); |
672
|
|
|
foreach ($id as $s_id) { |
673
|
|
|
if (empty($part)) |
674
|
|
|
unset($this->aRule[$s_id]); |
675
|
|
|
else { |
676
|
|
|
if (!is_array($part)) |
677
|
|
|
$part = array($part); |
678
|
|
|
foreach ($part as $s_part) |
679
|
|
|
unset($this->aRule[$s_id][$s_part]); |
680
|
|
|
} |
681
|
|
|
} |
682
|
|
|
} |
683
|
|
|
return $this; |
684
|
|
|
} // end of func ResetRule |
685
|
|
|
|
686
|
|
|
|
687
|
|
|
/** |
688
|
|
|
* Set default config |
689
|
|
|
*/ |
690
|
|
|
protected function SetCfgDefault () { |
691
|
|
|
parent::SetCfgDefault(); |
692
|
|
|
|
693
|
|
|
$this->SetCfg('id-prefix', 'validate_'); |
|
|
|
|
694
|
|
|
|
695
|
|
|
// User custom additional css define, can overwrite GetCss() |
696
|
|
|
$this->SetCfg('css-add', ''); |
|
|
|
|
697
|
|
|
|
698
|
|
|
// Check when input is disabled |
699
|
|
|
// Disabled input will be submit, needn't check in common. |
700
|
|
|
$this->SetCfg('check-disabled', false); |
|
|
|
|
701
|
|
|
|
702
|
|
|
// jQuery selector for form, empty for no submit check. |
703
|
|
|
$this->SetCfg('form-selector', 'form'); |
|
|
|
|
704
|
|
|
// Disable submit button for some time when clicked. |
705
|
|
|
$this->SetCfg('form-submit-delay', 3); |
|
|
|
|
706
|
|
|
|
707
|
|
|
// Func to show error msg |
708
|
|
|
// empty: Means no error msg, only set red border |
709
|
|
|
// alert: Using javascipt original alert() |
710
|
|
|
// jsalert: Using js to show msg in a float div |
711
|
|
|
// JsAlert: Using Fwolflib::js::JsAlert() |
712
|
|
|
// other: User defined js function(not implement). |
713
|
|
|
$this->SetCfg('func-show-error', ''); |
|
|
|
|
714
|
|
|
|
715
|
|
|
// Notice: Using JsAlert() now, jsalert is alost useless. |
716
|
|
|
// Setting for func jsalert |
717
|
|
|
$this->SetCfg('func-jsalert-close', '继续'); |
|
|
|
|
718
|
|
|
$this->SetCfg('func-jsalert-legend', 'Form validate fail'); |
|
|
|
|
719
|
|
|
// JsAlert css |
720
|
|
|
$this->SetCfg('func-jsalert-class', 'alert_fail'); |
|
|
|
|
721
|
|
|
$this->SetCfg('func-jsalert-css-div', ' |
|
|
|
|
722
|
|
|
left: 0px; |
723
|
|
|
position: absolute; |
724
|
|
|
text-align: center; |
725
|
|
|
top: 200px; |
726
|
|
|
width: 99%; |
727
|
|
|
z-index: 999; |
728
|
|
|
'); |
729
|
|
|
$this->SetCfg('func-jsalert-css-bg', ' |
|
|
|
|
730
|
|
|
background: #E5E5E5; |
731
|
|
|
filter: alpha(opacity=60); |
732
|
|
|
height: 100%; |
733
|
|
|
left: 0px; |
734
|
|
|
opacity: 0.6; |
735
|
|
|
position: absolute; |
736
|
|
|
top: 0px; |
737
|
|
|
width: 100%; |
738
|
|
|
z-index: 998; |
739
|
|
|
'); |
740
|
|
|
$this->SetCfg('func-jsalert-css-fieldset', ' |
|
|
|
|
741
|
|
|
background: white; |
742
|
|
|
border: 1px solid red; |
743
|
|
|
font-weight: bold; |
744
|
|
|
margin: auto; |
745
|
|
|
margin-top: -10em; |
746
|
|
|
padding-bottom: 2em; |
747
|
|
|
padding-top: 2em; |
748
|
|
|
width: 40%; |
749
|
|
|
'); |
750
|
|
|
$this->SetCfg('func-jsalert-css-legend', ' |
|
|
|
|
751
|
|
|
color: red; |
752
|
|
|
font-weight: bold; |
753
|
|
|
font-size: 1.1em; |
754
|
|
|
margin-left: 2em; |
755
|
|
|
margin-right: 2em; |
756
|
|
|
'); |
757
|
|
|
$this->SetCfg('func-jsalert-css-li', ' |
|
|
|
|
758
|
|
|
list-style: square; |
759
|
|
|
'); |
760
|
|
|
|
761
|
|
|
// Hint for input |
762
|
|
|
$this->SetCfg('hint-class', 'required'); |
|
|
|
|
763
|
|
|
$this->SetCfg('hint-text', '*'); |
|
|
|
|
764
|
|
|
|
765
|
|
|
// Path of arrow img in tip |
766
|
|
|
$this->SetCfg('path-img-arrow' |
|
|
|
|
767
|
|
|
, P2R . 'images/validate-arrow.png'); |
768
|
|
|
$this->SetCfg('path-img-loading' |
|
|
|
|
769
|
|
|
, P2R . 'images/validate-loading.gif'); |
770
|
|
|
|
771
|
|
|
// Show error in these event ? |
772
|
|
|
// Each id can overwrite these default setting by SetRule(). |
773
|
|
|
// Notice: If show-error-blur is true, and focus change from |
774
|
|
|
// 1 input to 2 both validate false, there will be dup alert |
775
|
|
|
// msg occur, bcs confirm alert will cause blur of 2nd input. |
776
|
|
|
$this->SetCfg('show-error-blur', false); |
|
|
|
|
777
|
|
|
$this->SetCfg('show-error-keyup', false); |
|
|
|
|
778
|
|
|
|
779
|
|
|
// Focus error input after show error |
780
|
|
|
$this->SetCfg('show-error-focus', false); |
|
|
|
|
781
|
|
|
|
782
|
|
|
// Tips distance from mouse |
783
|
|
|
$this->SetCfg('tip-offset-x', -20); |
|
|
|
|
784
|
|
|
$this->SetCfg('tip-offset-y', -60); |
|
|
|
|
785
|
|
|
// Text between col name and tip, |
786
|
|
|
// when auto prepend col name to tip. |
787
|
|
|
$this->SetCfg('tip-separator', ': '); |
|
|
|
|
788
|
|
|
|
789
|
|
|
return $this; |
790
|
|
|
} // end of func SetCfgDefault |
791
|
|
|
|
792
|
|
|
|
793
|
|
|
/** |
794
|
|
|
* Set validate rule |
795
|
|
|
* |
796
|
|
|
* @param mixed $id Str(, split) or array of str. |
797
|
|
|
* @param array $ar_cfg |
798
|
|
|
* @see $aRule |
799
|
|
|
* @return this |
800
|
|
|
*/ |
801
|
|
|
public function SetRule ($id, $ar_cfg) { |
802
|
|
|
// Id check, convert, trim |
803
|
|
|
if (empty($id)) |
804
|
|
|
return $this; |
805
|
|
|
if (!is_array($id)) |
806
|
|
|
// String, maybe ',' splitted |
807
|
|
|
$id = explode(',', $id); |
808
|
|
|
array_walk($id, create_function('&$v', '$v = trim($v);')); |
809
|
|
|
|
810
|
|
|
foreach ($id as $s_id) { |
811
|
|
|
// Id empty after explode ? |
812
|
|
|
if (empty($s_id)) |
813
|
|
|
continue; |
814
|
|
|
|
815
|
|
|
$this->aRule[$s_id]['id'] = $s_id; |
816
|
|
|
|
817
|
|
|
// Rule: append |
818
|
|
|
if (isset($ar_cfg['rule'])) { |
819
|
|
|
if (!is_array($ar_cfg['rule'])) |
820
|
|
|
$ar_cfg['rule'] = array($ar_cfg['rule']); |
821
|
|
|
foreach ($ar_cfg['rule'] as $s_rule) |
822
|
|
|
$this->aRule[$s_id]['rule'][] = $s_rule; |
823
|
|
|
} |
824
|
|
|
|
825
|
|
|
// Other part: overwrite |
826
|
|
|
foreach (array('tip', 'show-error-blur' |
827
|
|
|
, 'show-error-keyup') as $s) |
828
|
|
|
if (isset($ar_cfg[$s])) |
829
|
|
|
$this->aRule[$s_id][$s] = $ar_cfg[$s]; |
830
|
|
|
} |
831
|
|
|
|
832
|
|
|
return $this; |
833
|
|
|
} // end of func SetRule |
834
|
|
|
|
835
|
|
|
|
836
|
|
|
/** |
837
|
|
|
* Set validate rule, old way |
838
|
|
|
* for easy use and back compative. |
839
|
|
|
* |
840
|
|
|
* @param mixed $id Str or array of str. |
841
|
|
|
* @param mixed $rule Str or array of rule. |
842
|
|
|
* @param string $s_tip Rule info, or tips etc. |
843
|
|
|
* @param this |
844
|
|
|
*/ |
845
|
|
|
public function SetRuleV1 ($id, $rule, $s_tip = '') { |
846
|
|
|
if (empty($id) || empty($rule)) |
847
|
|
|
return $this; |
848
|
|
|
|
849
|
|
|
if (!is_array($id)) |
850
|
|
|
$id = array($id); |
851
|
|
|
foreach ($id as $s_id) { |
852
|
|
|
$this->aRule[$s_id]['id'] = $s_id; |
853
|
|
|
if (!is_array($rule)) |
854
|
|
|
$rule = array($rule); |
855
|
|
|
foreach ($rule as $s_rule) |
856
|
|
|
$this->aRule[$s_id]['rule'][] = $s_rule; |
857
|
|
|
$this->aRule[$s_id]['tip'] = $s_tip; |
858
|
|
|
} |
859
|
|
|
|
860
|
|
|
return $this; |
861
|
|
|
} // end of func SetRuleV1 |
862
|
|
|
|
863
|
|
|
|
864
|
|
|
} // end of class Validator |
865
|
|
|
?> |
|
|
|
|
866
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.