-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleForm.php
More file actions
575 lines (513 loc) · 18.8 KB
/
SimpleForm.php
File metadata and controls
575 lines (513 loc) · 18.8 KB
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
<?php
/*
* I hate to do this but I can not create a class variable to be accessed within
* a function nested within a method
*/
$GLOBALS['phoneFormats'] = array(
'xxx-xxx-xxxx',
'xxx xxx xxxx',
'(xxx)xxx xxxx',
'(xxx) xxx xxxx',
'(xxx)xxx-xxxx',
);
/**
* SimpleForms is a class that is designed to:
* - Make creating forms simple and intuitive
* - Make validation easy
* - Provide everyone with an easy to use form system
*
* YOU MUST START A SESSION ABOVE WHERE YOU USE THIS CLASS. I LEFT OUT AUTO STARTING A
* SESSION IN THE CLASS DUE TO SCHOOL SERVER ISSUES.
*
* All data is stored in the session under $_SESSION[$_formName . "Values"]
*
* If in the constructor you set $nameAttributeValue = "contactForm" and you create an input called
* input1, your data can be accessed by $_SESSION['contactFormValues']['input1'] or you can
* use getValue('input1') to avoid errors if value has yet to be set.
*/
class SimpleForm
{
// Things about form
private $_formName;
private $_formMethod;
private $_formAction;
private $_isValid;
// Error messages
private $_errorMessages = array();
// Data about form
private $_formElementList = array();
private $_notRequired = array();
/**
* @var string
* Compiled Error Message
*/
public $error = "";
/**
* @var array
* Array of user input
*/
public $values = array();
/**
* @var array
* List of available validation types
*/
public static $VALIDATION_TYPES = array(
'standardText',
'numbersOnly',
'textOnly',
'tagsAllowed',
'phoneNumber',
'email',
'zipCode',
'radio',
'select',
'submit'
);
/**
* Constructor used to set values of form
*
* @param $formName
* The name of your form. Used to store information about form to be accessed latter.
* @param $formMethod
* The method of handling data.
* Options are: "post" or "get".
* @param $formAction
* The action of the form. Used to redirect to new page after everything is valid.
* Defaults to a blank string so it stays on the same page.
*/
function SimpleForm($formName, $formMethod = "post", $formAction = "")
{
$this->_formName = $formName;
$this->_formMethod = $formMethod;
$this->_formAction = $formAction;
$this->_isValid = true;
}
/**
* Initiates new form on page and sets method to $formMethod. Also store get or post values in session.
*
* @see SimpleForm
*/
function startForm()
{
if ($this->_formMethod == "get") {
$this->values = $_GET;
} else {
$this->values = $_POST;
}
echo "<form action='' method='{$this->_formMethod}'>";
}
/**
* Placed at end of form to : close HTML tags, store form data
*/
function endForm()
{
echo '</form>';
// Store form element list for later use
$_SESSION[$this->_formName] = serialize($this);
}
/**
* Double quotes are used for all attributes ie. name="someName" or onclick="someFunction('foo')" (notice the
* single quotes when passing in the function parameters.)
*
* @param $nameAttributeValue
* Assigned to name="$nameAttributeValue"
* @param $label
* Label tags are placed before the input tag
* @param $errorMessage
* Error message to be displayed if invalid
* @param $typeOfValidation
* The type of validation that will be done to the element.
* @param $additionalAttributes
* Array of additional attributes formatted like so.. array("onclick" => "someFunction();",
* "class" => "class1 class2")
*
* @see validationTypes
*/
function inputText($nameAttributeValue, $label, $errorMessage = "Enter Text", $typeOfValidation = "standardText", $additionalAttributes = array())
{
// Add input to list of form elements for validation.
$this->_formElementList[$nameAttributeValue] = $typeOfValidation;
// Add error to list of form errors
$this->_errorMessages[$nameAttributeValue] = $errorMessage;
echo <<<HTML
<label>{$label}</label>
<input type="text" name="{$nameAttributeValue}" value="
HTML;
if (isset($this->values[$nameAttributeValue])) {
echo $this->values[$nameAttributeValue];
}
echo '"';
foreach ($additionalAttributes as $attribute => $value) {
echo ' ' . $attribute . '="' . $value . '"';
}
echo '>';
}
/**
* Double quotes are used for all attributes ie. name="someName" or onclick="someFunction('foo')" (notice the
* single quotes when passing in the function parameters.)
*
* @param $nameAttributeValue
* Assigned to name="$nameAttributeValue"
* @param $label
* Label tags are placed before the input tag
* @param $errorMessage
* Error message to be displayed if invalid
* @param string $typeOfValidation
* The type of validation that will be done to the element.
* @param array $additionalAttributes
* Array of additional attributes formatted like so.. array("onclick" => "someFunction();",
* "class" => "class1 class2")
*/
function inputTextArea($nameAttributeValue, $label, $errorMessage = "Enter Text", $typeOfValidation = "standardText", $additionalAttributes = array())
{
// Add input to list of form elements for validation.
$this->_formElementList[$nameAttributeValue] = $typeOfValidation;
// Add error to list of form errors
$this->_errorMessages[$nameAttributeValue] = $errorMessage;
echo <<<HTML
<label>{$label}</label>
<textarea type="text" name="{$nameAttributeValue}"
HTML;
foreach ($additionalAttributes as $attribute => $value) {
echo ' ' . $attribute . '="' . $value . '"';
}
echo '>';
if (isset($this->values[$nameAttributeValue])) {
echo $this->values[$nameAttributeValue];
}
echo '</textarea>';
}
/**
* Double quotes are used for all attributes ie. name="someName" or onclick="someFunction('foo')" (notice the
* single quotes when passing in the function parameters.)
*
* @param $nameAttributeValue
* Assigned to name="$nameAttributeValue"
* @param $label
* Label tags are placed before the input tag
* @param $optionsArray
* array("Label 1" => 1, "Label2" => "value2")
* @param bool $eachItemOnOwnLine
* Determines whether to break after each option or not
* @param array $additionalAttributes
* Array of additional attributes formatted like so.. array("onclick" => "someFunction();",
* "class" => "class1 class2")
* @param $errorMessage
* Error message to be displayed if invalid
*/
function inputRadio($nameAttributeValue, $label, $optionsArray, $eachItemOnOwnLine = false,
$additionalAttributes = array(), $errorMessage = "Select Radio")
{
$set = false;
// Add input to list of form elements for validation.
$this->_formElementList[$nameAttributeValue] = 'radio';
// Add error to list of form errors
$this->_errorMessages[$nameAttributeValue] = $errorMessage;
echo <<<HTML
<label>{$label}</label>
HTML;
foreach ($optionsArray as $label => $value) {
echo <<<HTML
<input type="radio" name="{$nameAttributeValue}" value="{$value}"
HTML;
foreach ($additionalAttributes as $attribute => $attributeValue) {
echo ' ' . $attribute . '="' . $attributeValue . '"';
}
if (isset($this->values[$nameAttributeValue]) && !$set) {
if ($this->values[$nameAttributeValue] == $value) {
echo ' checked="checked"';
$set = true;
}
} else {
if (!$set) {
echo ' checked="checked"';
$set = true;
}
}
echo <<<HTML
>{$label}
HTML;
if ($eachItemOnOwnLine) {
echo '<br>';
}
}
}
/**
* Double quotes are used for all attributes ie. name="someName" or onclick="someFunction('foo')" (notice the
* single quotes when passing in the function parameters.)
*
* @param $nameAttributeValue
* Assigned to name="$nameAttributeValue"
* @param $label
* Label tags are placed before the input tag
* @param $optionsArray
* array("Label 1" => 1, "Label2" => "value2")
* @param array $additionalAttributes
* Array of additional attributes formatted like so.. array("onclick" => "someFunction();",
* "class" => "class1 class2")
* @param $errorMessage
* Error message to be displayed if invalid
*/
function inputSelect($nameAttributeValue, $label, $optionsArray, $additionalAttributes = array(),
$errorMessage = "Select Dropdown")
{
$set = false;
// Add input to list of form elements for validation.
$this->_formElementList[$nameAttributeValue] = 'select';
// Add error to list of form errors
$this->_errorMessages[$nameAttributeValue] = $errorMessage;
echo <<<HTML
<label>{$label}</label>
<select name="{$nameAttributeValue}"
HTML;
foreach ($additionalAttributes as $attribute => $attributeValue) {
echo ' ' . $attribute . '="' . $attributeValue . '"';
}
echo '>';
foreach ($optionsArray as $label => $value) {
echo <<<HTML
<option value="{$value}"
HTML;
if (isset($this->values[$nameAttributeValue]) && !$set) {
if ($this->values[$nameAttributeValue] == $value) {
echo ' selected="selected"';
$set = true;
}
} else {
if (!$set) {
echo ' selected="selected"';
$set = true;
}
}
echo <<<HTML
>{$label}</option>
HTML;
}
echo '</select>';
}
/**
* @param $label
* Label is what the button says
* @param array $additionalAttributes
* Array of additional attributes formatted like so.. array("onclick" => "someFunction();",
* "class" => "class1 class2")
*/
function inputSubmit($label, $additionalAttributes = array())
{
// Add input to list of form elements for validation.
$this->_formElementList['submit'] = 'submit';
echo <<<HTML
<br>
<input type="submit" name="submit" value="{$label}"
HTML;
foreach ($additionalAttributes as $attribute => $value) {
echo ' ' . $attribute . '="' . $value . '"';
}
echo '>';
}
//============================================= Validation ====================================================/
/**
* @param $arrayOfNotRequiredInputNames
* An array of the names of elements that are not required.
*/
public function notRequired($arrayOfNotRequiredInputNames)
{
(array)$this->_notRequired = (array)$arrayOfNotRequiredInputNames;
}
/**
* @param $arrayOfPhoneFormats
* An array of phone formats. Format strings are composed of x's. ie: array('xxx-xxx-xxxx', 'xxxxxxxxxx')
* @param $keepStandardFormats
* Merge with defaults or replace with only your values
*/
public function setPhoneFormats($arrayOfPhoneFormats, $keepStandardFormats = true)
{
if ($keepStandardFormats) {
(array)$GLOBALS['phoneFormats'] = array_merge((array)$GLOBALS['phoneFormats'], (array)$arrayOfPhoneFormats);
(array)$GLOBALS['phoneFormats'] = array_unique((array)$GLOBALS['phoneFormats']);
} else {
(array)$GLOBALS['phoneFormats'] = (array)$arrayOfPhoneFormats;
}
}
/**
* @return array
* Returns an array of allowed phone formats.
*/
public function getPhoneFormats()
{
settype($GLOBALS['phoneFormats'], 'array');
return $GLOBALS['phoneFormats'];
}
/**
* @param string $functionToBeCalledIfInvalid
* Function to be executed if form is not valid
* @param string $functionToBeCalledIfValid
* Function to be executed if form is valid
*
* Should come after endForm
*
* @see endForm
*/
public function validate($functionToBeCalledIfInvalid = "_showError", $functionToBeCalledIfValid = "_redirect")
{
function _showError($errorMessage)
{
echo $errorMessage;
}
function _redirect($where)
{
header("Location: " . $where);
}
function standardText($name, &$value, $errorMessages, &$error, &$isValid)
{
$value = trim($value);
$value = strip_tags($value);
if (empty($value)) {
$error .= $errorMessages[$name] . '<br>';
$isValid = false;
}
}
function numbersOnly($name, &$value, $errorMessages, &$error, &$isValid)
{
$value = trim($value);
$value = strip_tags($value);
if (empty($value) || !isset($value) || preg_match("/([^0-9])+/", $value)) {
$error .= $errorMessages[$name] . '<br>';
$isValid = false;
}
}
function textOnly($name, &$value, $errorMessages, &$error, &$isValid)
{
$value = trim($value);
$value = strip_tags($value);
if (empty($value) || !isset($value) || is_numeric($value) || preg_match("^[0-9]^", $value)) {
$error .= $errorMessages[$name] . '<br>';
$isValid = false;
}
}
function tagsAllowed($name, &$value, $errorMessages, &$error, &$isValid)
{
$value = trim($value);
if (empty($value)) {
$error .= $errorMessages[$name] . '<br>';
$isValid = false;
}
}
function phoneNumber($name, &$value, $errorMessages, &$error, &$isValid)
{
$value = trim($value);
$value = strip_tags($value);
$formatted = preg_replace("^[0-9]^", "x", $value);
if (!in_array($formatted, $GLOBALS['phoneFormats'])) {
$error .= $errorMessages[$name] . '<br>';
$isValid = false;
} else {
// Remove everything except numbers
$value = preg_replace('/[^\d]/', '', $value);
// If like 555-5555 (seven characters) or 555-555-5555 (ten characters) format
if (strlen($value) == 7) {
$value = substr($value, 0, 3) . '-' . substr($value, 3);
} else if (strlen($value) == 10) {
$value = substr($value, 0, 3) . '-' . substr($value, 3, 3) . '-' . substr($value, 6);
}
}
}
function email($name, &$value, $errorMessages, &$error, &$isValid)
{
$value = trim($value);
$value = strip_tags($value);
if (!preg_match("/^([a-zA-Z0-9])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/", $value)) {
$error .= $errorMessages[$name] . '<br>';
$isValid = false;
}
}
function zipCode($name, &$value, $errorMessages, &$error, &$isValid)
{
$value = trim($value);
$value = preg_replace("[^0-9]", "", $value);
if (strlen($value) != 5) {
$error .= $errorMessages[$name] . '<br>';
$isValid = false;
}
}
function radio($name, &$value, $errorMessages, &$error, &$isValid)
{
if (!isset($value)) {
$error .= $errorMessages[$name] . '<br>';
$isValid = false;
}
}
function select($name, &$value, $errorMessages, &$error, &$isValid)
{
if (!isset($value)) {
$error .= $errorMessages[$name] . '<br>';
$isValid = false;
}
}
function submit($name, &$value, $errorMessages, &$error, &$isValid)
{
if (!isset($value)) {
$error .= $errorMessages[$name] . '<br>';
$isValid = false;
}
}
// If form has been submitted
if (isset($this->values['submit'])) {
// Go through each form element
foreach ($this->_formElementList as $name => $formElementType) {
// Go through each type of element
if (in_array($name, $this->_notRequired) && empty($this->values[$name])) {
continue;
}
foreach (SimpleForm::$VALIDATION_TYPES as $validationType) {
// If the current element is a real type
if ($validationType == $formElementType) {
// Check if element exists
if (isset($this->values[$name])) {
// Else validate
$validationType($name, $this->values[$name], $this->_errorMessages,
$this->error, $this->_isValid); //The function for validation is called based upon the type the
//User passed in
}
}
}
}
if ($this->_isValid) {
if ($functionToBeCalledIfValid == "_redirect") {
$functionToBeCalledIfValid($this->_formAction);
} else {
$functionToBeCalledIfValid();
}
} else {
if ($functionToBeCalledIfInvalid == "_showError") {
$functionToBeCalledIfInvalid($this->error);
} else {
$functionToBeCalledIfInvalid();
}
}
}
}
/**
* @param $formElementName
* Name of form element you are trying to access
* @return mixed
* Value of form element
*/
public function getValue($formElementName)
{
if (isset($this->values[$formElementName])) {
return $this->values[$formElementName];
}
return '';
}
private function is_assoc_array($array)
{
if (is_array($array) && !is_numeric(array_shift(array_keys($array)))) {
return true;
}
return false;
}
}
//TODO: Figure out why the formatted data is not being displayed back in the text fields
//TODO: Add more options for validation
//TODO: Find a way to do checkboxes efficiently