Classe permettant de gérer des formulaires

Classe PHP 5 et PHP 7, de formulaire. Permet de gérer des formulaires. Un exemple complet d'utilisation est fourni dans le script.

Information sur les mises à jour

Dernière mise à jour :

18 Août 2019
fonctionnement du code vérifié

4 837  vues
Compatibilité du code
PHP 7
  code classé dans   Classes
  code source classé dans   Classes
 
01
02
03
04
05
06
07
08
09
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
                    
<?php
/*------------------------------*/
/*
Titre : Classe permettant de gérer des formulaires

Date édition : 18 Sept 2011
Date mise a jour : 18 Aout 2019

Rapport de la maj:
- fonctionnement du code vérifié
*/
/*------------------------------*/
class form {



// propriétés privées : tous les éléments et attributs
// utilisables
// (certaines valeurs sont entrées par défaut)
private $eventArr = array ('onfocus' => '',
'onblur' => '',
'onselect' => '',
'onchange' => '',
'onclick' => '',
'ondblclick' => '',
'onmousedown' => '',
'onmouseup' => '',
'onmouseover' => '',
'onmousemove' => '',
'onmouseout' => '',
'onkeypress' => '',
'onkeydown' => '',
'onkeyup' => '');
private $commonArr = array ('id' => '',
'class' => '',
'title' => '',
'style' => '',
'dir' => '',
'lang' => '',
'xml:lang' => '');
private $formArr = array (
'method' => 'post',
'action' => '',
'id' => 'mainForm',
'enctype' => 'application/x-www-form-urlencoded',
'accept' => '',
'onsubmit' => '',
'onreset' => '',
'accept-charset' => 'unknown',
'style' => ''
);
private $inputArr = array ('text' => array ('value' => '',
'name' => '',
'alt' => '',
'tabindex' => '',
'accesskey' => '',
'readonly' => '',
'disabled' => '',
'width' => '',
'maxlength' => ''),
'button' => array ('name' => '',
'value' => '',
'alt' => '',
'tabindex' => '',
'accesskey' => '',
'disabled' => ''),
'hidden' => array ('name' => '',
'value' => '',
'alt' => '',
'disabled' => ''),
'password' => array ('name' => '',
'value' => '',
'alt' => '',
'tabindex' => '',
'accesskey' => '',
'readonly' => '',
'disabled' => '',
'width' => '',
'maxlength' => ''),
'submit' => array ('name' => '',
'value' => '',
'alt' => '',
'tabindex' => '',
'accesskey' => '',
'disabled' => ''),
'checkbox' => array ('name' => '',
'value' => '',
'alt' => '',
'tabindex' => '',
'accesskey' => '',
'disabled' => '',
'checked' => ''),
'radio' => array ('name' => '',
'value' => '',
'alt' => '',
'tabindex' => '',
'accesskey' => '',
'disabled' => '',
'checked' => '',
'title' => ''),
'reset' => array ('name' => '',
'class' => '',
'value' => '',
'alt' => '',
'tabindex' => '',
'accesskey' => '',
'disabled' => '',
'title' => ''),
'file' => array ('name' => '',
'value' => '',
'alt' => '',
'tabindex' => '',
'accesskey' => '',
'disabled' => '',
'accept' => ''),
'image' => array ('name' => '',
'value' => '',
'alt' => '',
'tabindex' => '',
'accesskey' => '',
'disabled' => '',
'src' => '',
'usemap' => '',
'ismap' => '')
);
private $fieldsetArr = array ();
private $legendArr = array ();
private $labelArr = array ('for' => '');
private $textareaArr = array ('rows' => '',
'cols' => '',
'disabled' => '',
'readonly' => '',
'accesskey' => '',
'tabindex' => '',
'name' => '');
private $selectArr = array ('disabled' => '',
'multiple' => '',
'size' => '',
'name' => '');
private $optionArr = array ('disabled' => '',
'label' => '',
'selected' => '',
'value' => '');
private $optgroupArr = array ('disabled' => '');
private $formBuffer = array ();
private $formElementArr = array ();
private $formAttributeArr = array ();

//Constructeur
public function __construct () {

}

// débuter effectivement le formulaire
public function openForm ($arrArgs = array ()) {
foreach ($this -> formArr as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formAttributeArr[$clef] = $arrArgs[$clef];
}
else if (!empty ($val)) {
$this -> formAttributeArr[$clef] = $val;
}
}
$this -> formBuffer['open'] = '<form ';
foreach ($this -> formAttributeArr as $clef => $val) {
$this -> formBuffer['open'] .= $clef.'="'.$val.'" ';
}
$this -> formBuffer['open'] .= '>';
}

// fermer le formulaire
public function closeForm () {
$this -> formBuffer['close'] = '</form>';
}

// ajouter un type input
public function addInput ($elem, $arrArgs = array ()) {
if (!array_key_exists ($elem, $this -> inputArr)) {
throw new Exception ($elem.' n\'est pas un élément valide');
}
if (!array_key_exists ('name', $arrArgs)
&& $elem !== 'submit'
&& $elem !== 'reset') {
$arrArgs['name'] = 'default';
}
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt][$elem] = array ();
$arrTemp = array_merge ($this -> eventArr, $this -> commonArr,
$this -> inputArr[$elem]);
foreach ($arrTemp as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formElementArr[$cpt][$elem][$clef] = $arrArgs[$clef];
}
}
$chaineTemp = '<input type="'.$elem.'" ';
foreach ($this -> formElementArr[$cpt][$elem] as $clef => $val) {
$chaineTemp .= $clef.'="'.$val.'" ';
}
$chaineTemp .= '/>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}

// ouvrir un fieldset
public function openFieldset ($arrArgs = array ()) {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['fieldset'] = array ();
$arrTemp = array_merge ($this -> eventArr, $this -> commonArr,
$this -> fieldsetArr);
foreach ($arrTemp as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formElementArr[$cpt]['fieldset'][$clef] = $arrArgs[$clef];
}
}
$chaineTemp = '<fieldset ';
foreach ($this -> formElementArr[$cpt]['fieldset'] as $clef => $val) {
$chaineTemp .= $clef.'="'.$val.'" ';
}
$chaineTemp .= '>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}

// fermer un fieldset
public function closeFieldset () {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['/fieldset'] = array ();
$chaineTemp = '</fieldset>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}

// ajouter une légende
public function addLegend ($legend, $arrArgs = array ()) {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['legend']['innerHTML'] = $legend;
$arrTemp = array_merge ($this -> eventArr, $this -> commonArr,
$this -> legendArr);
foreach ($arrTemp as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formElementArr[$cpt]['legend'][$clef] = $arrArgs[$clef];
}
}
$chaineTemp = '<legend ';
foreach ($this -> formElementArr[$cpt]['legend'] as $clef => $val) {
if ($clef !== 'innerHTML') {
$chaineTemp .= $clef.'="'.$val.'" ';
}
}
$chaineTemp .= '>'.$legend.'</legend>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}

// ouvrir une balise p
public function openP ($arrArgs = array ()) {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['p'] = array ();
$arrTemp = array_merge ($this -> eventArr, $this -> commonArr,
$this -> pArr);
foreach ($arrTemp as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formElementArr[$cpt]['p'][$clef] = $arrArgs[$clef];
}
}
$chaineTemp = '<p ';
foreach ($this -> formElementArr[$cpt]['p'] as $clef => $val) {
$chaineTemp .= $clef.'="'.$val.'" ';
}
$chaineTemp .= '>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}

// fermer une balise p
public function closeP () {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['/p'] = array ();
$chaineTemp = '</p>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}

// ajouter un label
public function addLabel ($label, $arrArgs = array ()) {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['label']['innerHTML'] = $label;
$arrTemp = array_merge ($this -> eventArr, $this -> commonArr,
$this -> labelArr);
foreach ($arrTemp as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formElementArr[$cpt]['label'][$clef] = $arrArgs[$clef];
}
}
$chaineTemp = '<label ';
foreach ($this -> formElementArr[$cpt]['label'] as $clef => $val) {
if ($clef !== 'innerHTML') {
$chaineTemp .= $clef.'="'.$val.'" ';
}
}
$chaineTemp .= '>'.$label.'</label>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}

// ajouter un textarea
public function addTextarea ($txt, $arrArgs = array ()) {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['textarea']['innerHTML'] = $txt;
$arrTemp = array_merge ($this -> eventArr, $this -> commonArr,
$this -> textareaArr);
foreach ($arrTemp as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formElementArr[$cpt]['textarea'][$clef] = $arrArgs[$clef];
}
}
$chaineTemp = '<textarea ';
foreach ($this -> formElementArr[$cpt]['textarea'] as $clef => $val) {
if ($clef !== 'innerHTML') {
$chaineTemp .= $clef.'="'.$val.'" ';
}
}
$chaineTemp .= '>'.$txt.'</textarea>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}

// ouvrir un select
public function openSelect ($arrArgs = array ()) {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['select'] = array ();
$arrTemp = array_merge ($this -> eventArr, $this -> commonArr,
$this -> selectArr);
foreach ($arrTemp as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formElementArr[$cpt]['select'][$clef] = $arrArgs[$clef];
}
}
$chaineTemp = '<select ';
foreach ($this -> formElementArr[$cpt]['select'] as $clef => $val) {
$chaineTemp .= $clef.'="'.$val.'" ';
}
$chaineTemp .= '>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}

// fermer un select
public function closeSelect () {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['/select'] = array ();
$chaineTemp = '</select>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}

// ajouter une option
public function addOption ($txt, $arrArgs = array ()) {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['option']['innerHTML'] = $txt;
$arrTemp = array_merge ($this -> eventArr, $this -> commonArr,
$this -> optionArr);
foreach ($arrTemp as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formElementArr[$cpt]['option'][$clef] = $arrArgs[$clef];
}
}
$chaineTemp = '<option ';
foreach ($this -> formElementArr[$cpt]['option'] as $clef => $val) {
if ($clef !== 'innerHTML') {
$chaineTemp .= $clef.'="'.$val.'" ';
}
}
$chaineTemp .= '>'.$txt.'</option>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}

// ouvrir un optgroup
public function openOptgroup ($label, $arrArgs = array ()) {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['optgroup']['label'] = $label;
$arrTemp = array_merge ($this -> eventArr, $this -> commonArr,
$this -> optgroupArr);
foreach ($arrTemp as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formElementArr[$cpt]['select'][$clef] = $arrArgs[$clef];
}
}
$chaineTemp = '<optgroup ';
foreach ($this -> formElementArr[$cpt]['optgroup'] as $clef => $val) {
$chaineTemp .= $clef.'="'.$val.'" ';
}
$chaineTemp .= '>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}

// fermer un optgroup
public function closeOptgroup () {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['/optgroup'] = array ();
$chaineTemp = '</optgroup>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}

// ajouter n'importe quoi
public function addAnything ($any) {
$cpt = count ($this -> formElementArr);
$this -> formBuffer['anything'][$cpt] = $any;
}


// méthode magique utilisée pour afficher effectivement le formulaire
// défini
public function __toString () {
$chaineTemp = $this -> formBuffer['open'];
foreach ($this -> formBuffer['elements'] as $clef => $val) {
if (isset ($this -> formBuffer['anything'][$clef])) {
$chaineTemp .= $this -> formBuffer['anything'][$clef];
}
$chaineTemp .= $val;
}
$chaineTemp .= $this -> formBuffer['close'];
return $chaineTemp;
}

// méthode pour libérer les ressources et créer un nouveau formulaire
// (tout formulaire réé auparavant et non affiché sera perdu)
public function freeForm () {
$this -> formBuffer = array ();
$this -> formElementArr = array ();
$this -> formAttributeArr = array ();
}

// destructeur (en attendant mieux...)
public function __destruct () {
unset ($this);
}

/***************************
***METHODS FOR DEBUGGING***
***************************/

// méthode affichant tous les éléments que contient le formulaire
public function showElems () {
$chaineTemp = '';
foreach ($this -> formElementArr as $clef => $val) {
foreach ($val as $elem => $attrArr) {
if (strpos ($elem, '/') !== false) {
$chaineTemp .= '<ul><li style="color: blue;">
end '.substr ($elem, 1, strlen ($elem)).'
</li></ul>';
}
else {
$chaineTemp .= '<ul><li style="color: blue;">'.$elem.'</li><ul>';
foreach ($attrArr as $attr => $value) {
$chaineTemp .= '<li style="color: red;">
'.$attr.' =
<span style="color: green; font-style: italic;">
'.$value.'</span></li>';
}
$chaineTemp .= '</ul></ul>';
}
}
}
return $chaineTemp;
}

// méthode coomptant les éléments que contient le formulaire :
// total global, et total par élément
public function countElems () {
foreach ($this -> formElementArr as $clef => $val) {
foreach ($val as $elem => $attrArr) {
if (strpos ($elem, '/') === false) {
$arrTemp[] = $elem;
}
}
}
$cptElem = count ($arrTemp);
$arrEachElem = array_count_values ($arrTemp);
$chaineTemp = '<span style="color: black; font-weight: bold;">
Total éléments : <span style="color: red;">
'.$cptElem.'</span><br />dont : </span><br />';
ksort ($arrEachElem, SORT_STRING);
foreach ($arrEachElem as $elem => $nbr) {
$chaineTemp .= '<span style="color: blue; margin-left: 20px;">
'.$elem.' : </span><span style="color: red;">
'.$nbr.'</span><br />';
}
return $chaineTemp;
}

}

// on instancie notre objet
$form = new form ();
// on crée un 1er formulaire
$form -> openForm
(array ('action' => '?', 'id' => 'MyForm'));
$form -> openFieldset
(array ('style' => 'border:1px dotted red; width: 300px;'));
$form -> addLegend
('test');
$form -> addInput
('text', array ('id' => 'MyText', 'value' => 'ok', 'test'=>'test'));
$form -> addLabel
('label', array ('for' => 'MyText', 'style' => 'margin: 5px;'));
$form -> addAnything
('<br /><br />');
$form -> addInput
('button',array('id' =>'MyButton','value'=>'click!','test'=>'test'));
$form -> closeFieldset ();
$form -> closeForm ();

echo '<div style="border:1px solid darkgrey;text-align:center;width:310px;">';
// on l'affiche
echo $form;
echo '</div>';

// on compte et affiche ses éléments (debugging only)
echo $form -> showElems ();
echo $form -> countElems ();

// on libère les ressources pour pouvoir créer un 2d formulaire
$form -> freeForm ();

// on réinitialise un nouveau formulaire
// on ouvre effectivement le nouveau formulaire
$form -> openForm
(array ('action' => '?', 'id' => 'MyForm2'));
$form -> openFieldset
(array ('style' => 'border:1px dotted blue; width: 300px;'));
$form -> addLegend ('test 2');
$form -> addInput
('text',array ('id' => 'MyText2','value' => 'yep','test' =>'test'));
$form -> addInput
('checkbox', array ('id' => 'MyCheck','value'=>'1','test'=>'test'));
$form -> addLabel ('Checkbox', array ('for' => 'MyCheck'));
$form -> addTextarea ('mon texte', array ('cols' => 20, 'rows' => 10));
$form -> openSelect ();
$form -> openOptgroup ('label options 1');
$form -> addOption ('1');
$form -> closeOptgroup ();
$form -> openOptgroup ('label options 2');
$form -> addOption ('2');
$form -> addOption ('2_2');
$form -> closeOptgroup ();
$form -> closeSelect ();
$form -> closeFieldset ();
$form -> closeForm ();

echo '<div style="border:1px solid orange;text-align:center; width:310px;">';
echo $form;
echo '</div>';

echo $form -> showElems ();
echo $form -> countElems ();

$form -> freeForm ();

?>
<?php
/*------------------------------*/
/*
Titre : Classe permettant de gérer des formulaires

Date édition : 18 Sept 2011
Date mise a jour : 18 Aout 2019

Rapport de la maj:
- fonctionnement du code vérifié
*/
/*------------------------------*/
class form {



// propriétés privées : tous les éléments et attributs
// utilisables
// (certaines valeurs sont entrées par défaut)
private $eventArr = array ('onfocus' => '',
'onblur' => '',
'onselect' => '',
'onchange' => '',
'onclick' => '',
'ondblclick' => '',
'onmousedown' => '',
'onmouseup' => '',
'onmouseover' => '',
'onmousemove' => '',
'onmouseout' => '',
'onkeypress' => '',
'onkeydown' => '',
'onkeyup' => '');
private $commonArr = array ('id' => '',
'class' => '',
'title' => '',
'style' => '',
'dir' => '',
'lang' => '',
'xml:lang' => '');
private $formArr = array (
'method' => 'post',
'action' => '',
'id' => 'mainForm',
'enctype' => 'application/x-www-form-urlencoded',
'accept' => '',
'onsubmit' => '',
'onreset' => '',
'accept-charset' => 'unknown',
'style' => ''
);
private $inputArr = array ('text' => array ('value' => '',
'name' => '',
'alt' => '',
'tabindex' => '',
'accesskey' => '',
'readonly' => '',
'disabled' => '',
'width' => '',
'maxlength' => ''),
'button' => array ('name' => '',
'value' => '',
'alt' => '',
'tabindex' => '',
'accesskey' => '',
'disabled' => ''),
'hidden' => array ('name' => '',
'value' => '',
'alt' => '',
'disabled' => ''),
'password' => array ('name' => '',
'value' => '',
'alt' => '',
'tabindex' => '',
'accesskey' => '',
'readonly' => '',
'disabled' => '',
'width' => '',
'maxlength' => ''),
'submit' => array ('name' => '',
'value' => '',
'alt' => '',
'tabindex' => '',
'accesskey' => '',
'disabled' => ''),
'checkbox' => array ('name' => '',
'value' => '',
'alt' => '',
'tabindex' => '',
'accesskey' => '',
'disabled' => '',
'checked' => ''),
'radio' => array ('name' => '',
'value' => '',
'alt' => '',
'tabindex' => '',
'accesskey' => '',
'disabled' => '',
'checked' => '',
'title' => ''),
'reset' => array ('name' => '',
'class' => '',
'value' => '',
'alt' => '',
'tabindex' => '',
'accesskey' => '',
'disabled' => '',
'title' => ''),
'file' => array ('name' => '',
'value' => '',
'alt' => '',
'tabindex' => '',
'accesskey' => '',
'disabled' => '',
'accept' => ''),
'image' => array ('name' => '',
'value' => '',
'alt' => '',
'tabindex' => '',
'accesskey' => '',
'disabled' => '',
'src' => '',
'usemap' => '',
'ismap' => '')
);
private $fieldsetArr = array ();
private $legendArr = array ();
private $labelArr = array ('for' => '');
private $textareaArr = array ('rows' => '',
'cols' => '',
'disabled' => '',
'readonly' => '',
'accesskey' => '',
'tabindex' => '',
'name' => '');
private $selectArr = array ('disabled' => '',
'multiple' => '',
'size' => '',
'name' => '');
private $optionArr = array ('disabled' => '',
'label' => '',
'selected' => '',
'value' => '');
private $optgroupArr = array ('disabled' => '');
private $formBuffer = array ();
private $formElementArr = array ();
private $formAttributeArr = array ();

//Constructeur
public function __construct () {

}

// débuter effectivement le formulaire
public function openForm ($arrArgs = array ()) {
foreach ($this -> formArr as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formAttributeArr[$clef] = $arrArgs[$clef];
}
else if (!empty ($val)) {
$this -> formAttributeArr[$clef] = $val;
}
}
$this -> formBuffer['open'] = '<form ';
foreach ($this -> formAttributeArr as $clef => $val) {
$this -> formBuffer['open'] .= $clef.'="'.$val.'" ';
}
$this -> formBuffer['open'] .= '>';
}

// fermer le formulaire
public function closeForm () {
$this -> formBuffer['close'] = '</form>';
}

// ajouter un type input
public function addInput ($elem, $arrArgs = array ()) {
if (!array_key_exists ($elem, $this -> inputArr)) {
throw new Exception ($elem.' n\'est pas un élément valide');
}
if (!array_key_exists ('name', $arrArgs)
&& $elem !== 'submit'
&& $elem !== 'reset') {
$arrArgs['name'] = 'default';
}
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt][$elem] = array ();
$arrTemp = array_merge ($this -> eventArr, $this -> commonArr,
$this -> inputArr[$elem]);
foreach ($arrTemp as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formElementArr[$cpt][$elem][$clef] = $arrArgs[$clef];
}
}
$chaineTemp = '<input type="'.$elem.'" ';
foreach ($this -> formElementArr[$cpt][$elem] as $clef => $val) {
$chaineTemp .= $clef.'="'.$val.'" ';
}
$chaineTemp .= '/>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}

// ouvrir un fieldset
public function openFieldset ($arrArgs = array ()) {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['fieldset'] = array ();
$arrTemp = array_merge ($this -> eventArr, $this -> commonArr,
$this -> fieldsetArr);
foreach ($arrTemp as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formElementArr[$cpt]['fieldset'][$clef] = $arrArgs[$clef];
}
}
$chaineTemp = '<fieldset ';
foreach ($this -> formElementArr[$cpt]['fieldset'] as $clef => $val) {
$chaineTemp .= $clef.'="'.$val.'" ';
}
$chaineTemp .= '>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}

// fermer un fieldset
public function closeFieldset () {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['/fieldset'] = array ();
$chaineTemp = '</fieldset>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}

// ajouter une légende
public function addLegend ($legend, $arrArgs = array ()) {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['legend']['innerHTML'] = $legend;
$arrTemp = array_merge ($this -> eventArr, $this -> commonArr,
$this -> legendArr);
foreach ($arrTemp as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formElementArr[$cpt]['legend'][$clef] = $arrArgs[$clef];
}
}
$chaineTemp = '<legend ';
foreach ($this -> formElementArr[$cpt]['legend'] as $clef => $val) {
if ($clef !== 'innerHTML') {
$chaineTemp .= $clef.'="'.$val.'" ';
}
}
$chaineTemp .= '>'.$legend.'</legend>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}

// ouvrir une balise p
public function openP ($arrArgs = array ()) {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['p'] = array ();
$arrTemp = array_merge ($this -> eventArr, $this -> commonArr,
$this -> pArr);
foreach ($arrTemp as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formElementArr[$cpt]['p'][$clef] = $arrArgs[$clef];
}
}
$chaineTemp = '<p ';
foreach ($this -> formElementArr[$cpt]['p'] as $clef => $val) {
$chaineTemp .= $clef.'="'.$val.'" ';
}
$chaineTemp .= '>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}

// fermer une balise p
public function closeP () {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['/p'] = array ();
$chaineTemp = '</p>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}

// ajouter un label
public function addLabel ($label, $arrArgs = array ()) {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['label']['innerHTML'] = $label;
$arrTemp = array_merge ($this -> eventArr, $this -> commonArr,
$this -> labelArr);
foreach ($arrTemp as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formElementArr[$cpt]['label'][$clef] = $arrArgs[$clef];
}
}
$chaineTemp = '<label ';
foreach ($this -> formElementArr[$cpt]['label'] as $clef => $val) {
if ($clef !== 'innerHTML') {
$chaineTemp .= $clef.'="'.$val.'" ';
}
}
$chaineTemp .= '>'.$label.'</label>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}

// ajouter un textarea
public function addTextarea ($txt, $arrArgs = array ()) {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['textarea']['innerHTML'] = $txt;
$arrTemp = array_merge ($this -> eventArr, $this -> commonArr,
$this -> textareaArr);
foreach ($arrTemp as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formElementArr[$cpt]['textarea'][$clef] = $arrArgs[$clef];
}
}
$chaineTemp = '<textarea ';
foreach ($this -> formElementArr[$cpt]['textarea'] as $clef => $val) {
if ($clef !== 'innerHTML') {
$chaineTemp .= $clef.'="'.$val.'" ';
}
}
$chaineTemp .= '>'.$txt.'</textarea>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}

// ouvrir un select
public function openSelect ($arrArgs = array ()) {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['select'] = array ();
$arrTemp = array_merge ($this -> eventArr, $this -> commonArr,
$this -> selectArr);
foreach ($arrTemp as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formElementArr[$cpt]['select'][$clef] = $arrArgs[$clef];
}
}
$chaineTemp = '<select ';
foreach ($this -> formElementArr[$cpt]['select'] as $clef => $val) {
$chaineTemp .= $clef.'="'.$val.'" ';
}
$chaineTemp .= '>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}

// fermer un select
public function closeSelect () {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['/select'] = array ();
$chaineTemp = '</select>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}

// ajouter une option
public function addOption ($txt, $arrArgs = array ()) {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['option']['innerHTML'] = $txt;
$arrTemp = array_merge ($this -> eventArr, $this -> commonArr,
$this -> optionArr);
foreach ($arrTemp as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formElementArr[$cpt]['option'][$clef] = $arrArgs[$clef];
}
}
$chaineTemp = '<option ';
foreach ($this -> formElementArr[$cpt]['option'] as $clef => $val) {
if ($clef !== 'innerHTML') {
$chaineTemp .= $clef.'="'.$val.'" ';
}
}
$chaineTemp .= '>'.$txt.'</option>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}

// ouvrir un optgroup
public function openOptgroup ($label, $arrArgs = array ()) {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['optgroup']['label'] = $label;
$arrTemp = array_merge ($this -> eventArr, $this -> commonArr,
$this -> optgroupArr);
foreach ($arrTemp as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formElementArr[$cpt]['select'][$clef] = $arrArgs[$clef];
}
}
$chaineTemp = '<optgroup ';
foreach ($this -> formElementArr[$cpt]['optgroup'] as $clef => $val) {
$chaineTemp .= $clef.'="'.$val.'" ';
}
$chaineTemp .= '>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}

// fermer un optgroup
public function closeOptgroup () {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['/optgroup'] = array ();
$chaineTemp = '</optgroup>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}

// ajouter n'importe quoi
public function addAnything ($any) {
$cpt = count ($this -> formElementArr);
$this -> formBuffer['anything'][$cpt] = $any;
}


// méthode magique utilisée pour afficher effectivement le formulaire
// défini
public function __toString () {
$chaineTemp = $this -> formBuffer['open'];
foreach ($this -> formBuffer['elements'] as $clef => $val) {
if (isset ($this -> formBuffer['anything'][$clef])) {
$chaineTemp .= $this -> formBuffer['anything'][$clef];
}
$chaineTemp .= $val;
}
$chaineTemp .= $this -> formBuffer['close'];
return $chaineTemp;
}

// méthode pour libérer les ressources et créer un nouveau formulaire
// (tout formulaire réé auparavant et non affiché sera perdu)
public function freeForm () {
$this -> formBuffer = array ();
$this -> formElementArr = array ();
$this -> formAttributeArr = array ();
}

// destructeur (en attendant mieux...)
public function __destruct () {
unset ($this);
}

/***************************
***METHODS FOR DEBUGGING***
***************************/

// méthode affichant tous les éléments que contient le formulaire
public function showElems () {
$chaineTemp = '';
foreach ($this -> formElementArr as $clef => $val) {
foreach ($val as $elem => $attrArr) {
if (strpos ($elem, '/') !== false) {
$chaineTemp .= '<ul><li style="color: blue;">
end '.substr ($elem, 1, strlen ($elem)).'
</li></ul>';
}
else {
$chaineTemp .= '<ul><li style="color: blue;">'.$elem.'</li><ul>';
foreach ($attrArr as $attr => $value) {
$chaineTemp .= '<li style="color: red;">
'.$attr.' =
<span style="color: green; font-style: italic;">
'.$value.'</span></li>';
}
$chaineTemp .= '</ul></ul>';
}
}
}
return $chaineTemp;
}

// méthode coomptant les éléments que contient le formulaire :
// total global, et total par élément
public function countElems () {
foreach ($this -> formElementArr as $clef => $val) {
foreach ($val as $elem => $attrArr) {
if (strpos ($elem, '/') === false) {
$arrTemp[] = $elem;
}
}
}
$cptElem = count ($arrTemp);
$arrEachElem = array_count_values ($arrTemp);
$chaineTemp = '<span style="color: black; font-weight: bold;">
Total éléments : <span style="color: red;">
'.$cptElem.'</span><br />dont : </span><br />';
ksort ($arrEachElem, SORT_STRING);
foreach ($arrEachElem as $elem => $nbr) {
$chaineTemp .= '<span style="color: blue; margin-left: 20px;">
'.$elem.' : </span><span style="color: red;">
'.$nbr.'</span><br />';
}
return $chaineTemp;
}

}

// on instancie notre objet
$form = new form ();
// on crée un 1er formulaire
$form -> openForm
(array ('action' => '?', 'id' => 'MyForm'));
$form -> openFieldset
(array ('style' => 'border:1px dotted red; width: 300px;'));
$form -> addLegend
('test');
$form -> addInput
('text', array ('id' => 'MyText', 'value' => 'ok', 'test'=>'test'));
$form -> addLabel
('label', array ('for' => 'MyText', 'style' => 'margin: 5px;'));
$form -> addAnything
('<br /><br />');
$form -> addInput
('button',array('id' =>'MyButton','value'=>'click!','test'=>'test'));
$form -> closeFieldset ();
$form -> closeForm ();

echo '<div style="border:1px solid darkgrey;text-align:center;width:310px;">';
// on l'affiche
echo $form;
echo '</div>';

// on compte et affiche ses éléments (debugging only)
echo $form -> showElems ();
echo $form -> countElems ();

// on libère les ressources pour pouvoir créer un 2d formulaire
$form -> freeForm ();

// on réinitialise un nouveau formulaire
// on ouvre effectivement le nouveau formulaire
$form -> openForm
(array ('action' => '?', 'id' => 'MyForm2'));
$form -> openFieldset
(array ('style' => 'border:1px dotted blue; width: 300px;'));
$form -> addLegend ('test 2');
$form -> addInput
('text',array ('id' => 'MyText2','value' => 'yep','test' =>'test'));
$form -> addInput
('checkbox', array ('id' => 'MyCheck','value'=>'1','test'=>'test'));
$form -> addLabel ('Checkbox', array ('for' => 'MyCheck'));
$form -> addTextarea ('mon texte', array ('cols' => 20, 'rows' => 10));
$form -> openSelect ();
$form -> openOptgroup ('label options 1');
$form -> addOption ('1');
$form -> closeOptgroup ();
$form -> openOptgroup ('label options 2');
$form -> addOption ('2');
$form -> addOption ('2_2');
$form -> closeOptgroup ();
$form -> closeSelect ();
$form -> closeFieldset ();
$form -> closeForm ();

echo '<div style="border:1px solid orange;text-align:center; width:310px;">';
echo $form;
echo '</div>';

echo $form -> showElems ();
echo $form -> countElems ();

$form -> freeForm ();

?>

      Fonctions du code - Doc officielle PHP

   php.net  
Description
Versions PHP
    array
Crée un tableau
PHP 4, 5, 7 et 8
    array_key_exists
Vérifie si une clé existe dans un tableau
PHP 4, 5, 7 et 8
    array_merge
Fusionne plusieurs tableaux en un seul
PHP 4, 5, 7 et 8
    count
Compte tous les éléments d'un tableau ou dans un objet Countable
PHP 4, 5, 7 et 8
    empty
Détermine si une variable est vide
PHP 4, 5, 7 et 8

[3]

  • avatar

    Administrateur

    02 Août 2019 à 17:49

    Lorsque la classe est utilisée avec PHP 7+, supprimer la méthode __destruct () devrait résoudre le problème.

  • avatar

    Invité

    02 Août 2019 à 15:26

    bonjour j ai ce code d'erreur Fatal error: Cannot unset $this in /var/www/html/test/index.php on line 401

  • avatar

    Aroybase

    21 Jan 2011 à 15:36

    Je n'ai pas testé, mais ça m'a l'air fichtrement sympatique !

Minimum 10 mots. Votre commentaire sera visible après validation.


 Autres snippets qui pourraient vous intéresser

Classe DATABASE - PHP Sources

Compatibilité : PHP 8

Classe pour se connecter en PHP 8 avec des améliorations majeures + 10 exemples

Classe MySQL - PHP Sources

Compatibilité : PHP 4, PHP 5

Cette classe de MySQL a les caractéristiques suivantes: Les messages de débogage (peut être activer ou non) et les traitement des erreurs.

* Requêtes exécutées avec Recherche Contextuelle

  Les derniers scripts

PHP 8.5.5

logo PHP
Langue langue us
Date 12 Avril
Taille 32 Mo
Catégorie PHP

PHP 8.4.20

logo PHP
Langue langue us
Date 12 Avril
Taille 30 Mo
Catégorie PHP

Serendipity 2.6.0

logo Serendipity
Langue langue fr
Date 11 Avril
Taille 15 Mo
Catégorie Blogs

Drupal 11.3.6

logo Drupal
Langue langue us
Date 11 Avril
Taille 34 Mo
Catégorie CMS

TYPO3 14.2.0

logo TYPO3
Langue langue fr
Date 10 Avril
Taille 38 Mo
Catégorie CMS

Dolibarr ERP 23.0.1

logo Dolibarr ERP
Langue langue fr
Date 09 Avril
Taille 89 Mo
Catégorie Logiciels

  18 Sept 2011

Information sur les mises à jour

Dernière mise à jour :

18 Août 2019
fonctionnement du code vérifié

4 837 Vues
Compatibilité du code
PHP 7