Menu

[7138ce]: / form2.class.php  Maximize  Restore  History

Download this file

595 lines (590 with data), 19.1 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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
<?php
/**
* @file form2.class.php
* @author Ferenc Szontágh <szontagh.ferenc@gmail.com>
*
* @section LICENSE
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
* @section DESCRIPTION
*
* Generate Programmatically HTML forms with PHP<br/>
* Tested on PHP 5.5.12 with HTML5<br/>
* Compatible with <a href="http://getbootstrap.com/">Bootstrap</a>
*
*/
class form2 {
/**
* Variable to store the form_name
*/
private $form_name;
/**
* object list with the added inputs
*/
private $forms;
/**
* counter for internal input ids
*/
private $counter = 0;
/**
* generate auto input id, or use the users's
*/
private $auto_id = true;
/**
* the method of the form
*/
private $method = "POST";
/**
* Append to the current div container
*/
private $append = false;
/**
* collected from $_POST
*/
private $_post;
/**
* collected from $_GET
*/
private $_get;
/**
*
* name: form2::__construct
* Constructor for set up the default values
* @param $form_name The name of the form
* @param $method The method of the form
* @param $auto_id TRUE: Generate auto input id, FALSE: do not generate auto id
* @return void
*
*/
function __construct($form_name = null, $method = "POST", $auto_id = true) {
$this->auto_id = $auto_id;
$this->form_name = $form_name;
// if no form name present, then we generate a one
if ($this->form_name === null) {
$this->form_name = md5(date("Y"));
}
$this->method = $method;
$this->_get = $_GET;
$this->_post = $_POST;
}
/**
*
* name: form2::addResetButton
* Add a reset button to the form, alias to form2::addInput method with predefined parameters
* @param $name Name of the reset button
* @param $label The string of the button
* @return object form_input The reset button object
*
*/
public function addResetButton($name, $label) {
return $this->addInput("reset", $name, null, $label, null);
}
/**
*
* name: form2::addButton
* Add a button to the form, alias to form2::addInput method with predefined parameters
* @param $name The name of the button
* @param $label The string of the button
* @return object form_input The button object
*
*/
public function addButton($name, $label) {
return $this->addInput("button", $name, null, $label, null);
}
/**
*
* name: form2::addInput
* Add a new input to the form
* @param $type The type of the input
* @param $name The name of the input
* @param $label String into the label tag, if omitted, no label generated
* @param $value The default value of the input, if omitted, no value added
* @param $id The input id
* @return object form_input The input object
*
*/
public function addInput($type, $name, $label = null, $value = null, $id = null) {
if ($id === null AND $this->auto_id === true) {
$id = $this->form_name . "_" . $name + "_" . md5($this->counter);
}
$this->forms[$this->counter] = new form_input($type, $name, $value, $id);
if ($title !== null) {
$this->forms[$this->counter]->addAttr("title", $label);
}
$last_id = $this->counter;
$this->counter++;
return $this->forms[$last_id];
}
/**
*
* name: form2::show
* Render & show the generated form
* @return string Return the generated HTML Form
*
*/
public function show() {
return $this->render();
}
/**
*
* name: form2::render
* Render the html from the added forms
* @return string Return the generated HTML Form
*
*/
private function render() {
$ret = "\n<form role=\"form\" method=\"" . $this->method . "\" name=\"" . $this->form_name . "\">";
$show_separator = true;
$types = new input_types();
foreach ($this->forms as $form_id => $form) {
$previous = isset($this->forms[($form_id - 1)]) ? $this->forms[($form_id - 1)] : null;
$prev_is_button = $previous === null ? false : ($types->isButton($previous->type));
$next = isset($this->forms[($form_id + 1)]) ? $this->forms[($form_id + 1)] : null;
$next_is_button = $next === null ? false : ($types->isButton($next->type));
$current_is_button = $types->isButton($form->type);
if ($prev_is_button === false AND $current_is_button === true) {
$ret .= "\t</div>";
}
if (($prev_is_button === false AND $current_is_button === true) OR $current_is_button === false) {
if ($form->type == "radio" OR $form->type == "checkbox") {
$ret .= "\n\t<div class=\"" . $form->type . "\">";
} else {
$ret .= "\n\t<div class=\"form-group\">";
}
}
$ret .= "\n\t\t\t" . $form->get() . "\n";
if ($next_is_button === false) {
$ret .= "\t</div>";
}
}
$ret .= "\n</form>";
return $ret;
}
/**
*
* name: form2::validate
* Validate the forms value with the specified rules
* @return bool Return true, when all form is filled with correct values
*
*/
public function validate() {
$type = new input_types();
if (count($this->_post) < 1) {
return false;
}
foreach ($this->forms as $form_id => $form) {
if ($type->isButton($form->type)) {
continue;
}
if (!isset($form->validate->type)) {
switch ($form->type) {
case "email":
$this->forms[$form_id]->addRule("regex", "/^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/", "INVALID_EMAIL");
break;
case "number":
$this->forms[$form_id]->addRule("regex", "/^[0-9]+$/", "INVALID_NUMBER");
break;
case "datetime":
case "datetime-local":
$this->forms[$form_id]->addRule("regex", "/^([0-9]{4}\.\ [0-9]{2}\.\ [0-9]{2}\.\ [\d]{2}\:[\d]{2})$/", "INVALID_DATETIME_FORMAT");
break;
}
}
if (isset($this->_post[$form->name])) {
if ($form->type != "select" AND $form->type != "radio" AND $form->type != "checkbox") {
$form->value = $this->_post[$form->name];
}
if ($form->type == "select" and isset($this->_post[$form->name])) {
$form->addAttr("selected", $this->_post[$form->name]);
}
if (($form->type == "radio" or $form->type == "checkbox") and isset($this->_post[$form->name])) {
if ($form->value == $this->_post[$form->name]) {
$form->addAttr("checked", "checked");
}
}
}
}
$status = null;
foreach ($this->forms as $form_id => $form) {
if ($type->isButton($form->type)) {
continue;
}
if (isset($form->validate->type)) {
switch ($form->validate->type) {
case "regex":
$check = preg_match($form->validate->context, $form->value, $t);
$check = $check == 1 ? true : false;
break;
case "callback":
$check = call_user_func($form->validate->context, $form->value);
break;
default:
$check = true;
}
if ($check === true AND $form->valid !== false) {
$form->valid = true;
} else {
$form->valid = false;
$form->error_str = $form->validate->error_str;
}
}
if ($form->getAttr("minlength") !== null) {
if (mb_strlen($form->value) < $form->getAttr("minlength")) {
$form->valid = false;
$form->error_str = "MIN_LENGTH";
} else {
if ($form->valid === null) {
$form->valid = true;
}
}
}
if ($form->getAttr("maxlength") !== null) {
if (mb_strlen($form->value) > $form->getAttr("maxlength")) {
$form->valid = false;
$form->error_str = "MAX_LENGTH";
} else {
if ($form->valid === null) {
$form->valid = true;
}
}
}
if ($form->getAttr("required") !== null) {
if (mb_strlen($form->value) < 1) {
$form->valid = false;
$form->error_str = "REQUIERED";
} else {
if ($form->valid === null) {
$form->valid = true;
}
}
}
if ($form->getAttr("min") != null) {
if ($form->value < $form->getAttr("min")) {
$form->valid = false;
$form->error_str = "NUM_LOWER_THAN_MIN";
} else {
if ($form->valid === null) {
$form->valid = true;
}
}
}
if ($form->getAttr("max") != null) {
if ($form->value > $form->getAttr("max")) {
$form->valid = false;
$form->error_str = "NUM_HIGHER_THAN_MAX";
} else {
if ($form->valid === null) {
$form->valid = true;
}
}
}
if ($form->valid === false) {
$status = false;
}
if ($status === null AND $form->valid === true) {
$status = true;
}
}
return $status;
}
}
/**
*
* Object for store and manipulate input elements
*
*/
class form_input {
/**
* type of the input
*/
public $type;
/**
* name of the input
*/
public $name;
/**
* value of the input
*/
public $value;
/**
* id of the input
*/
public $id;
/**
* object for store the validate info for the input
*/
public $validate = null;
/**
* other attributes for the input
*/
private $attrs;
/**
* defult class for inputs
*/
private $default_input_class = "form-control";
/**
* default class for buttons
*/
private $default_button_class = "btn btn-default";
/**
* default class for reset buttons
*/
private $default_button_reset_class = "btn btn-danger";
/**
* validated state of the input
*/
public $valid = null;
/**
* error string for the input
*/
public $error;
/**
*
* name: form_input::__construct
* Construct default parameters
* @param $type Type of the input
* @param $name Name of the input
* @param $value Value of the input
* @param $id Id of the input
*
*/
function __construct($type, $name, $value, $id) {
$this->type = $type;
$this->name = $name;
$this->value = $value;
$this->id = $id;
$this->attrs = new stdClass();
$this->validate = new stdClass();
}
/**
*
* name: form_input::addAttr
* Add attribute to the element
* @param $key name of the attribute, if is array, then the form_input::$value can be omitted
* @param $value value of the attribute
*
*/
public function addAttr($key, $value = null) {
if (is_array($key) AND $value === null) {
foreach ($key as $k => $v) {
$this->attrs->$k = $v;
}
} else {
$this->attrs->$key = $value;
}
}
/**
*
* name: form_input::removeAttr
* Remove attribute from the input
* @param $key Name of the attribute to remove
*
*/
public function removeAttr($key) {
if (isset($this->attrs->$key)) {
unset($this->attrs->$key);
}
}
/**
*
* name: form_input::getAttr
* Return the value of the input attribute, or null when the attribute not found
* @param $key Name of the attribute
* @return string Return the value of the attribute
*
*/
public function getAttr($key) {
if (isset($this->attrs->$key)) {
return $this->attrs->$key;
}
return null;
}
/**
*
* name: form_input::get
* Return the full formatted html input
* @return string Return the html output of the input
*
*/
public function get() {
$type = new input_types();
if (!isset($this->attrs->class) OR $this->attrs->class == null) {
if ($type->isLineInput($this->type)) {
$this->addAttr("class", $this->default_input_class);
}
if ($this->type == "button" or $this->type == "submit" or $this->type == "link") {
$this->addAttr("class", $this->default_button_class);
}
if ($this->type == "reset") {
$this->addAttr("class", $this->default_button_reset_class);
}
}
$d = "";
if ($this->getAttr("title") !== null AND $type->isButton($this->type) === false AND ($this->type != "radio") AND $this->type != "checkbox") {
$d .= "<label for=\"" . $this->id . "\">" . $this->getAttr("title") . "</label>";
}
if ($this->type == "radio" OR $this->type == "checkbox") {
$d .= "<label for=\"" . $this->id . "\">";
}
switch ($this->type) {
case "select":
$d .= "<select name=\"" . $this->name . "\"";
if (isset($this->id)) {
$d .= " id=\"" . $this->id . "\"";
}
break;
case "textarea":
$d .= "<textarea";
break;
case "button":
$d .= "<button name=\"" . $this->name . "\"";
break;
default:
$d .= "<input type=\"" . $this->type . "\"";
$d .= " name=\"" . $this->name . "\"";
if (!empty($this->value)) {
$d .= " value=\"" . $this->value . "\"";
}
if (!empty($this->id)) {
$d .= " id=\"" . $this->id . "\"";
}
}
$attr = get_object_vars($this->attrs);
if (is_array($attr)) {
foreach ($attr as $k => $v) {
if ($k == "required") {
$v = $v == true ? "required" : "";
}
$d .= " " . $k . "=\"" . $v . "\" ";
}
}
switch ($this->type) {
case "textarea":
$d .= ">" . $this->value . "</textarea>";
break;
case "button":
$d .= ">" . $this->value . "</button>";
break;
case "select":
$d .= ">";
if (isset($this->value) AND !empty($this->value)) {
$selected = $this->getAttr("selected");
foreach ($this->value as $vk => $vv) {
if ($selected !== null AND $selected == $vk) {
$d .= "<option selected=\"selected\" value=\"" . $vk . "\">" . $vv . "</option>";
} else {
$d .= "<option value=\"" . $vk . "\">" . $vv . "</option>";
}
}
}
$d .= "</select>";
break;
default:
$d .= "/>";
}
if ($this->type == "radio" OR $this->type == "checkbox") {
$d .= $this->getAttr("title");
$d .= "</label>";
}
if ($this->valid === false) {
$d .= "<p style='color: #FF0000'>" . $this->error_str . "</p>";
}
return $d;
}
/**
*
* name: form_input::addRule
* @param $type Type of the validate rule: regex,callback
* @param $context regex: then a regex pattern, callback: a function name
* @param $error_str The error string, when the pattern not match or the callback function return false
* @return
*
*/
public function addRule($type, $context, $error_str) {
$this->validate->type = $type;
$this->validate->context = $context;
$this->validate->error_str = $error_str;
}
}
/**
*
* Container object for storing input types
*
*/
class input_types {
private $button_inputs = array("button", "submit", "reset", "link");
private $line_inputs = array("text", "password", "datetime", "datetime-local", "date", "month", "time", "week", "number", "email", "url", "search", "tel", "color", "select");
private $hight_inputs = array("textarea", "multiselect");
/**
*
* name: input_types::getButtons
* @return array Array list of button input types
*
*/
public function getButtons() {
return $this->button_inputs;
}
/**
*
* name: input_types::getLineInputs
* @return array Array list of lined input types (like text,password)
*
*/
public function getLineInputs() {
return $this->line_inputs;
}
/**
*
* name: input_types::getHightInputs
* @return array Array list of high (tall) inputs
*
*/
public function getHightInputs() {
return $this->getHightInputs;
}
/**
*
* name: input_types::isButton
* @param $type Type name of an element
* @return bool true when it is a button, false when not
*
*/
public function isButton($type) {
return in_array($type, $this->button_inputs);
}
/**
*
* name: input_types::isLineInput
* @param $type Type name of an element
* @return bool true when it is a line input, false when not
*
*/
public function isLineInput($type) {
return in_array($type, $this->line_inputs);
}
/**
*
* name: input_types::isHightInput
* @param $type Type name of an element
* @return bool true when it is a high input, false when not
*
*/
public function isHightInput($type) {
return in_array($type, $this->hight_inputs);
}
}
?>
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.