-
Notifications
You must be signed in to change notification settings - Fork 31
/
validFluent.php
553 lines (467 loc) · 13.7 KB
/
validFluent.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
<?php
/**
*
* ValidFluent
*
* A simple, flexible and easy to use PHP form validation class
* (uses a fluent interface )
*
* @author Andre Soares
* andsoa77@gmail.com
*
* typical use:
$valid = new ValidFluent($_POST);
$valid->name('user_name')->required('You must choose a user name!')->alfa()->minSize(5);
$valid->name('user_email')->required()->email();
$valid->name('birthdate')->date('please enter date in YYYY-MM-DD format');
if ($valid->isGroupValid())
echo 'Validation Passed!';
* //////////////////////////////////////
* OR
*
$valid = new ValidFluent($_POST);
if ( $valid->name('user_name')->required('You must choose a user name!')->alfa()->minSize(5)
->name('user_email')->required()->email()
->name('birthdate')->date('please enter date in YYYY-MM-DD format')
->isGroupValid() )
echo 'Validation passed!';
* //////////////////////////////////////////////////////////////////
* On HTML
<form method="POST">
<input type="text" name="email"
value="<?php echo $valid->getValue('email'); ?>" />
<span class="error">
<?php echo $valid->getError('email'); ?>
</span>
...
...
* ///////////////////////////////////////////////////////////////////
* To create new validation rules!
#1 define default error message
private static $error_myValidaton = 'my default error message';
#2 create new validation function
function myValidation($param , $errorMsg=NULL)
{
if ($this->isValid && (! empty($this->currentObj->value)))
{
//
//code to check if validation pass
//
$this->isValid = // TRUE or FALSE ;
if (! $this->isValid)
$this->setErrorMsg($errorMsg, self::$error_myValidation, $param);
}
return $this;
}
#3 use it
$Valid->name('testing')->myValidation(10, 'some error msg!');
*
*
*/
/**
* helper class for ValidFluent
*/
class validFluentObj
{
public $value;
public $error;
function __construct($value)
{
$this->value = $value;
$this->error = '';
}
}
/**
*
*/
class ValidFluent
{
public $isValid = TRUE;
public $isGroupValid = TRUE;
public $validObjs; //array of validFluentObj
private $currentObj; //pointer to current validFluentObj , set by ->name()
//default error messages
private static $error_required = 'This field is required';
private static $error_date = 'Please enter a date in the YYYY-MM-DD format';
private static $error_email = 'Please enter a valid email';
private static $error_url = 'Please enter a valid url';
private static $error_alfa = 'Only letters and numbers are permited';
private static $error_text = 'Only letters are permited';
private static $error_minSize = 'Please enter more than %s characters';
private static $error_maxSize = 'Please enter less than %s characters';
private static $error_numberFloat = 'Only numbers are permitted';
private static $error_numberInteger = 'Only numbers are permitted';
private static $error_numberMax = 'Please enter a value lower than %s ';
private static $error_numberMin = 'Please enter a value greater than %s ';
private static $error_oneOf = 'Please choose one of " %s "';
private static $error_equal = 'Fields did not match';
private static $error_regex = 'Please choose a valid value';
// some regEx's
private $pattern_email = '/^([a-zA-Z0-9_\+\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/';
private $pattern_url = '/^((http|ftp|https):\/\/)?www\..*.\.\w\w\w?(\/.*)?(\?.*)?$/'; //check...
////private $pattern_alfa = '/^[a-zA-Z0-9_\-\. ]+$/';
private $pattern_alfa = '/^(\d|\-|_|\.| |(\p{L}\p{M}*))+$/u';
private $pattern_text = '/^( |(\p{L}\p{M}*)+$/u';
private $pattern_numberInteger = '/^[\+\-]?[0-9]+$/';
private $pattern_numberFloat = '/^[\+\-]?[0-9\.]+$/';
private $pattern_date = '/^(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$/';
/**
*
* @param Array $post ($Key => $value) array
*/
function __construct($post)
{
foreach ($post as $key => $value)
{
$this->validObjs[$key] = new validFluentObj(trim($value));
}
}
/**
* Helper: returns TRUE if last valiadtion passed , else FALSE
* @return Boolean
*/
function isValid()
{
return $this->isValid;
}
/**
* Helper: returns TRUE if all validations passed , else FALSE
* @return Boolean
*/
function isGroupValid()
{
return $this->isGroupValid;
}
/**
* Return's $name validation error
* @param string $name
* @return string the error
*/
function getError($name)
{
if (isset($this->validObjs[$name]))
return $this->validObjs[$name]->error;
return '';
}
/**
* Returs $name value
* @param string $name
* @return string the value
*/
function getValue($name)
{
if (isset($this->validObjs[$name]))
return $this->validObjs[$name]->value;
return '';
}
/**
* Used to set starting values on Form data
* ex: $valid->name('user_name)->setValue($database->getUserName() );
* @param string $value
*/
function setValue($value)
{
$this->currentObj->value = $value;
return $this;
}
/**
* used to set error messages out of the scope of validFluent
* ex: $valid->name('user_name')->setError('The Name "Andre" is already taken , please try another')
* @param string $error
*/
function setError($error)
{
$this->currentObj->error = $error;
$this->isGroupValid = FALSE;
$this->isValid = FALSE;
return $this;
}
/**
* PRIVATE Helper to set error messages
* @param string $errorMsg custom error message
* @param string $default default error message
* @param string $params extra parameter to default error message
*/
private function setErrorMsg($errorMsg, $default, $params=NULL)
{
$this->isGroupValid = FALSE;
if ($errorMsg == '')
{
$this->currentObj->error = sprintf($default, $params);
}
else
$this->currentObj->error = $errorMsg;
}
////////////////////////////////////////////////
///////////////////////////////////////////////
////
/// Validation Functions
//
/**
* Used to set a pointer for current validation object
* if $name doesnt exits, it will be created with a empty value
* note:validation always pass on empy not required fields
* @param string $name as in array($name => 'name value')
* @return ValidFluent
*/
function name($name)
{
if (!isset($this->validObjs[$name]))
$this->validObjs[$name] = new validFluentObj('');
$this->isValid = TRUE;
$this->currentObj = &$this->validObjs[$name];
return $this;
}
/**
* Note if field is required , then it must me called right after name!!
* ex: $valid->name('user_name')->required()->text()->minSize(5);
* @param string $errorMsg
* @return ValidFluent
*/
function required($errorMsg=NULL)
{
if ($this->isValid)
{
$this->isValid = ( $this->currentObj->value != '') ? TRUE : FALSE;
if (!$this->isValid)
$this->setErrorMsg($errorMsg, self::$error_required);
}
return $this;
}
/**
* validates a Date in yyyy-mm-dd format
* @param string $errorMsg
* @return ValidFluent
*/
function date($errorMsg=NULL)
{
if ($this->isValid && (!empty($this->currentObj->value)))
{
$this->isValid = (preg_match($this->pattern_date, $this->currentObj->value)) ? TRUE : FALSE;
if (!$this->isValid)
$this->setErrorMsg($errorMsg, self::$error_date);
}
return $this;
}
/**
* validates an email address
* @param string $errorMsg
* @return ValidFluent
*/
function email($errorMsg=NULL)
{
if ($this->isValid && (!empty($this->currentObj->value)))
{
$this->isValid = (preg_match($this->pattern_email, $this->currentObj->value) > 0) ? TRUE : FALSE;
if (!$this->isValid)
$this->setErrorMsg($errorMsg, self::$error_email);
}
return $this;
}
/**
* validates a URL address
* @param string $errorMsg
* @return ValidFluent
*/
function url($errorMsg=NULL)
{
if ($this->isValid && (!empty($this->currentObj->value)))
{
$this->isValid = (preg_match($this->pattern_url, $this->currentObj->value)) ? TRUE : FALSE;
if (!$this->isValid)
$this->setErrorMsg($errorMsg, self::$error_url);
}
return $this;
}
/**
* ex: ->regex('/^[^<>]+$/', 'ERROR: < and > arent valid characters')
* @param string $regex a regular expresion '/regex/'
* @param string $errorMsg
* @return ValidFluent
*/
function regex($regex, $errorMsg=NULL)
{
if ($this->isValid && (!empty($this->currentObj->value)))
{
$this->isValid = (preg_match($regex, $this->currentObj->value)) ? TRUE : FALSE;
if (!$this->isValid)
$this->setErrorMsg($errorMsg, self::$error_regex);
}
return $this;
}
/**
* ex: ->name('password')->equal('passwordConfirm' , 'passwords didnt match')
* @param string $value2
* @param string $errorMsg
* @return ValidFluent
*/
function equal($value2, $errorMsg=NULL)
{
if ($this->isValid && (!empty($this->currentObj->value)))
{
$this->isValid = ($value2 == $this->currentObj->value);
if (!$this->isValid)
$this->setErrorMsg($errorMsg, self::$error_equal);
}
return $this;
}
/**
* Ex: ->oneOf('blue:red:green' , 'only blue , red and green permited')
* *case insensitive*
* @param string $items ex: 'blue:red:green'
* @param string $errorMsg
* @return ValidFluent
*/
function oneOf($items, $errorMsg=NULL)
{
if ($this->isValid && (!empty($this->currentObj->value)))
{
$item = explode(':', strtolower($items));
$result = array_intersect($item, array(strtolower($this->currentObj->value)));
$this->isValid = (!empty($result));
if (!$this->isValid)
{
$itemsList = str_replace(':', ' / ', $items);
$this->setErrorMsg($errorMsg, self::$error_oneOf, $itemsList);
}
}
return $this;
}
/////////////////////////////////////////////////////
////////////////////////////////////////////////////
////
/// text validation
//
/**
* Only allows A-Z a-Z and space
* @param string $errorMsg
* @return ValidFluent
*/
function text($errorMsg=NULL)
{
if ($this->isValid && (!empty($this->currentObj->value)))
{
$this->isValid = (preg_match($this->pattern_text, $this->currentObj->value)) ? TRUE : FALSE;
if (!$this->isValid)
$this->setErrorMsg($errorMsg, self::$error_text);
}
return $this;
}
/**
* Only allows A-Z a-z 0-9 space and ( - . _ )
* @param string $errorMsg
* @return ValidFluent
*/
function alfa($errorMsg=NULL)
{
if ($this->isValid && (!empty($this->currentObj->value)))
{
$this->isValid = (preg_match($this->pattern_alfa, $this->currentObj->value)) ? TRUE : FALSE;
if (!$this->isValid)
$this->setErrorMsg($errorMsg, self::$error_alfa);
}
return $this;
}
// same function, better spelled
function alpha($errorMsg=NULL)
{
$this->alfa($errorMsg);
return $this;
}
/**
* @param int $size the maximum string size
* @param string $errorMsg
* @return ValidFluent
*/
function maxSize($size, $errorMsg=NULL)
{
if ($this->isValid && (!empty($this->currentObj->value)))
{
$this->isValid = (strlen($this->currentObj->value) <= $size);
if (!$this->isValid)
$this->setErrorMsg($errorMsg, self::$error_maxSize, $size);
}
return $this;
}
/**
* @param int $size the minimum string size
* @param string $errorMsg
* @return ValidFluent
*/
function minSize($size, $errorMsg=NULL)
{
if ($this->isValid && (!empty($this->currentObj->value)))
{
$this->isValid = (strlen($this->currentObj->value) >= $size);
if (!$this->isValid)
$this->setErrorMsg($errorMsg, self::$error_minSize, $size);
}
return $this;
}
/////////////////////////////////////////////////////
////////////////////////////////////////////////////
////
/// Numbers validation
//
/**
* checks if its a float ( + - . ) permited
* @param string $errorMsg
* @return ValidFluent
*/
function numberFloat($errorMsg=NULL)
{
if ($this->isValid && (!empty($this->currentObj->value)))
{
$this->isValid = (preg_match($this->pattern_numberFloat, $this->currentObj->value)) ? TRUE : FALSE;
if (!$this->isValid)
$this->setErrorMsg($errorMsg, self::$error_numberFloat);
}
return $this;
}
/**
* checks if its a integer ( + - ) permited
* @param string $errorMsg
* @return ValidFluent
*/
function numberInteger($errorMsg=NULL)
{
if ($this->isValid && (!empty($this->currentObj->value)))
{
$this->isValid = (preg_match($this->pattern_numberInteger, $this->currentObj->value)) ? TRUE : FALSE;
if (!$this->isValid)
$this->setErrorMsg($errorMsg, self::$error_numberInteger);
}
return $this;
}
/**
* @param number $max
* @param string $errorMsg
* @return ValidFluent
*/
function numberMax($max, $errorMsg=NULL)
{
if ($this->isValid && (!empty($this->currentObj->value)))
{
$this->isValid = ($this->currentObj->value <= $max) ? TRUE : FALSE;
if (!$this->isValid)
$this->setErrorMsg($errorMsg, self::$error_numberMax, $max);
}
return $this;
}
/**
* @param number $min
* @param string $errorMsg
* @return ValidFluent
*/
function numberMin($min, $errorMsg=NULL)
{
if ($this->isValid && (!empty($this->currentObj->value)))
{
$this->isValid = ($this->currentObj->value >= $min) ? TRUE : FALSE;
if (!$this->isValid)
$this->setErrorMsg($errorMsg, self::$error_numberMin, $min);
}
return $this;
}
}
?>