forked from ASoares/PHP-Form-Validation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
410 lines (387 loc) · 16.3 KB
/
index.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>PHP Form Validation</title>
<meta name="description" content="A PHP Form Validation class">
<meta name="author" content="50North 4West">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" href="css/styles.css?v=1.0">
</head>
<body>
<div class="container">
<div class="row mt-5">
<div class="col-12 text-center">
<h3 class="display-4">PHP Form Validation Class</h3>
</div>
</div>
<div class="row">
<div class="col-12 text-justify text-secondary">
<p>
A simple, flexible and easy to use PHP form validation class using fluent methodologies. Fluent design allows you to chain method calls, which results in less typed
characters when applying multiple operations on the same object. The validator has been forked from the orignal written by Andre Soares and found at
<a href="https://github.com/ASoares/PHP-Form-Validation">https://github.com/ASoares/PHP-Form-Validation</a>. This updated version is maintained by 50North 4West and can be
found at <a href="https://github.com/ASoares/PHP-Form-Validation">https://github.com/ASoares/PHP-Form-Validation</a>. This new version follows the PSR-2 Coding Standard,
uses PHP's native FILTER_VALIDATE_XXXX wherever possible and validates file uploads.
</p>
</div>
</div>
<div class="row">
<div class="col-12 text-justify text-secondary">
<p>
<b>Licence:</b> <a href="http://www.gnu.org/licenses/gpl-2.0.txt">GPL v2 (GNU GENERAL PUBLIC LICENSE)</a>.
</p>
</div>
</div>
<div class="row">
<div class="col-12 text-secondary">
<hr />
<h4>
<b>How to use:</b>
</h4>
<p>
<ul>
<li>
Inlcude the validate class using <code>include 'Validate.php';</code>
</li>
<li>
In your form processing script Instantiate/create the Validate object <code>$validate = new Validate($_POST, $_FILES); </code>
</li>
<li>
Set each fields checks by chaining together method calls (note: required should ALWAYS follow field name):
<ul>
<li>
<code>$validate->name('user_name')->required()->email();</code>
</li>
<li>
<code>$validate->name('password')->required()->alpha()->minSize(5);</code>
</li>
<li>
<code>$validate->name('password_confirm')->required()->alpha()->minSize(5)->equal('password');</code>
</li>
<li>
<code>$validate->fileName('file_upload')->fileSize(500000, 'custom error message')->fileType(array('image/jpeg', 'image/gif', 'text/xml'), 'custom error message');</code>
</li>
</ul>
</li>
<li>
You can check if the fields have validated with <code>if ($valid->isGroupValid()) {echo 'Validation Passed!';}</code>
</li>
<li>
You can get the custom or default error message's by using <code>echo $validate->getError('exampleInputNumber');</code>
</li>
<li>
For file uploads make sure you use getFileError <code>echo $validate->getFileError('file_upload');</code>
</li>
<li>
You can set or return the form fields' data with <code>value="echo $validate->getValue('exampleInputEmail');"</code>
</li>
</ul>
</p>
</div>
</div>
<div class="row">
<div class="col-12 text-secondary">
<hr />
<h4>
<b>Limitations:</b>
</h4>
<p>
The Alpha & Text validations use ctype_alnum & ctype_alpha which do not allow character spaces; if you want to validate a field with charater spaces use the Regex validation with <code>->regex('/^[a-z\d\-_\s]+$/i')</code>
</p>
</div>
</div>
<div class="row">
<div class="col-12 text-justify text-secondary">
<hr />
<h4>
<b>Form Example:</b>
</h4>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="card bg-light">
<div class="card-body">
<?php
//Form Processing Script
//include the Validate Class
include 'Validate.php';
//You can set the fields prior to submitting the form
if (empty($_POST)) {
//Instantiate the class
$validate = new Validate(array());
//Set a field value
$validate->name('exampleInputEmail')->setValue('[email protected]');
} else {
//validate the $_POST & $_FILES data
//Instantiate the class
$validate = new Validate($_POST, $_FILES);
//Set the field validations you want to do
$validate->name('exampleInputEmail')->required()->email();
$validate->name('exampleInputDate')->date('Y-m-d');
$validate->name('exampleInputNumber')->numberFloat()->maxSize(5);
$validate->name('exampleInputRequired')->required();
//when using a file upload field ensure that you use ->filename('fieldName') not ->name('fieldName')
$validate->fileName('exampleFormControlFile')->fileSize(50000000000)->fileType(array('image/jpeg', 'image/gif', 'image/png'));
//Give a general failed or success message
if ($validate->isGroupValid()): ?>
<div class="alert alert-success" role="alert">
Form was ok.
</div>
<?php else: ?>
<div class="alert alert-danger" role="alert">
There was something wrong on the form.
</div>
<?php endif;
}
?>
<form method="POST" enctype="multipart/form-data">
<div class="row">
<div class="col-6">
<div class="form-group">
<label for="exampleInputEmail">Example Email field (You can also set a value)</label>
<input type="email" class="form-control <?php echo ($validate->getError('exampleInputEmail')) ? 'is-invalid' : ''; ?>" name="exampleInputEmail" value="<?php echo $validate->getValue('exampleInputEmail'); ?>">
<div class="invalid-feedback"><?php echo $validate->getError('exampleInputEmail'); ?></div>
</div>
</div>
<div class="col-6">
<div class="form-group">
<label for="exampleInputDate">Example Date field</label>
<input type="date" class="form-control <?php echo ($validate->getError('exampleInputDate')) ? 'is-invalid' : ''; ?>" name="exampleInputDate" value="<?php echo $validate->getValue('exampleInputDate'); ?>">
<div class="invalid-feedback"><?php echo $validate->getError('exampleInputDate'); ?></div>
</div>
</div>
</div>
<div class="row">
<div class="col-6">
<div class="form-group">
<label for="exampleInputNumber">Example Number field (Float, max 5)</label>
<input type="number" class="form-control <?php echo ($validate->getError('exampleInputNumber')) ? 'is-invalid' : ''; ?>" name="exampleInputNumber" value="<?php echo $validate->getValue('exampleInputNumber'); ?>">
<div class="invalid-feedback"><?php echo $validate->getError('exampleInputNumber'); ?></div>
</div>
</div>
<div class="col-6">
<div class="form-group">
<label for="exampleInputRequired">Example Required field</label>
<input type="text" class="form-control <?php echo ($validate->getError('exampleInputRequired')) ? 'is-invalid' : ''; ?>" name="exampleInputRequired" value="<?php echo $validate->getValue('exampleInputRequired'); ?>">
<div class="invalid-feedback"><?php echo $validate->getError('exampleInputRequired'); ?></div>
</div>
</div>
</div>
<div class="row">
<div class="col-6">
<div class="form-group">
<label for="exampleFormControlFile">Example file input (jpeg or gif)</label>
<input type="file" class="form-control-file <?php echo ($validate->getFileError('exampleFormControlFile')) ? 'is-invalid' : ''; ?>" name="exampleFormControlFile[]" multiple>
<div class="invalid-feedback"><?php echo $validate->getFileError('exampleFormControlFile'); ?></div>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary mt-2">Submit</button>
</form>
</div>
</div>
</div>
</div>
<div class="row mt-4">
<div class="col-12 text-secondary">
<hr />
<h4>
<b>Available Validations:</b>
</h4>
<p>
You can create your own additional validations however the following lists are included in the Class. Please see further down the page for how to do this. All validations must start with
<code>$validate->name('fieldName')</code>. Most functions allow a custom error message passed to them, alternatively you can set default messages in the Class. Date formats use any acceptable PHP date format.
</p>
<div class="row">
<div class="col-md-6">
<p>
POST DATA
<ul>
<li>
Required: <code>->required('custom error message')</code>
</li>
<li>
Date: <code>->date('Y-m-d', 'custom error message')</code>
</li>
<li>
Time: <code>->date('H:i:s', 'custom error message')</code>
</li>
<li>
Email: <code>->email('custom error message')</code>
</li>
<li>
URL: <code>->url('custom error message')</code>
</li>
<li>
REGEX: <code>->regex('/^hello/', 'custom error message')</code>
</li>
<li>
Equal: <code>->equal('FieldNameOfFirstField', 'custom error message')</code>
</li>
<li>
OneOf: <code>->oneOf('blue:red:green', 'custom error message')</code>
</li>
<li>
Text: <code>->text('custom error message')</code> (only allows A-Z or a-Z)
</li>
<li>
Alpha: <code>->alpha('custom error message')</code> (alpha numeric)
</li>
<li>
Max Size: <code>->maxSize(5, 'custom error message')</code>
</li>
<li>
Min Size: <code>->minSize(6, 'custom error message')</code>
</li>
<li>
Number Max: <code>->numberMax(300, 'custom error message')</code>
</li>
<li>
Number Min: <code>->numberMin(50, 'custom error message')</code>
</li>
<li>
Number Float: <code>->numberFloat('custom error message')</code>
</li>
<li>
Number Interger: <code>->numberInteger('custom error message')</code>
</li>
</ul>
</p>
</div>
<div class="col-md-6">
<p>
FILES DATA
<ul>
<li>
File Size: <code>->fileSize(500000, 'custom error message')</code>
</li>
<li>
File Type: <code>->fileType(array('image/jpeg', 'image/gif'), 'custom error message')</code>
</li>
</ul>
The Class has access to the following $_FILES data:
<ul>
<li>
<code>$this->currentFileObject->fileName</code>
</li>
<li>
<code>$this->currentFileObject->type</code>
</li>
<li>
<code>$this->currentFileObject->tmpName</code>
</li>
<li>
<code>$this->currentFileObject->type</code>
</li>
<li>
<code>$this->currentFileObject->uploadError</code>
</li>
<li>
<code>$this->currentFileObject->size</code>
</li>
</ul>
</p>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-12 text-secondary">
<hr />
<h4>
<b>How to create new validation rules for $_POST data:</b>
</h4>
<p>
<ol>
<li>
Define the default error message.<br />
<pre>
<code>
private static $error_myValidaton = 'my default error message';
</code>
</pre>
</li>
<li>
Create a new validation function.<br />
<pre>
<code>
function myValidation($param , $errorMsg=NULL)
{
if ($this->isValid && (! empty($this->currentObject->value))) {
//
//Add your code to check if validation passes. You can use $this->currentObject->value to get the field value
//
$this->isValid = // TRUE or FALSE ;
if (! $this->isValid) {
$this->setErrorMsg($errorMsg, self::$error_myValidation, $param);
}
}
return $this;
}
</code>
</pre>
</li>
<li>
Use it in your code<br />
<code>$Valid->name('testing')->myValidation(10, 'some error msg!');</code>
</li>
</ol>
</p>
</div>
</div>
<div class="row">
<div class="col-12 text-secondary">
<hr />
<h4>
<b>How to create new validation rules for $_FILES data:</b>
</h4>
<p>
<ol>
<li>
Define the default error message.<br />
<pre>
<code>
private static $error_myValidaton = 'my default error message';
</code>
</pre>
</li>
<li>
Create a new validation function.<br />
<pre>
<code>
function myValidation($param , $errorMsg=NULL)
{
if ($this->isValid && (! empty($this->currentFileObject->value))) {
//
//Add your code to check if validation passes. You can use $this->currentFileObject->value to get the field value
//
$this->isValid = // TRUE or FALSE ;
if (! $this->isValid) {
$this->setFileErrorMsg($errorMsg, self::$error_myValidation, $param);
}
}
return $this;
}
</code>
</pre>
</li>
<li>
Use it in your code<br />
<code>$Valid->name('testing')->myValidation(10, 'some error msg!');</code>
</li>
</ol>
</p>
</div>
</div>
<hr />
<div class="row mb-5">
</div>
</div>
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
</body>
</html>