Salut à tous,
je suis en train de préparer un framework perso.
J'ai bientôt fini ma class concernant les formulaire, et je souhaitais avoir votre avis sur la manière dont je traite la classe, la syntaxe, enfin tout quoi !
Voici la class et merci d'avance pour votre aide.
Si besoin ai, je peux aussi mettre le code pour afficher un formulairea vec cette classe.
PS : Il manquera juste la classe de classe cryptage decryptage.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 <? class form { private $formArray = array(); private $submitName = 'submit'; //Nom du bouton submit private $method; private $action; private $indice = 0; private $title; private $resetButton; private $isFormValidated = FALSE; //Permet de savoir si le formulaire a été validé ou non const _KEY = 'jesussuperstar'; //METHODES MAGIQUES================================================================================ public function __construct ($method, $action) { $this->method=$method; $this->action=$action; } public function __tostring () { //Si le formulaire est validé on :----------------------------\ if(isSet($_POST['submit'])){ // - récupère dans le formArray les valeurs saisies $i=0; foreach($_POST as $key=>$tabValues) { //On fait attention à ne pas charger le boutton submit if ($key != $this->submitName) $this->formArray[$i]['value'] = $tabValues; $i++; } // - traite les erreurs s'il y a lieu $check = new checkFormValue($_POST,self::_KEY); $check -> checkFormInput($this); } //------------------------------------------------------------/ //ajout du titre si demandé if (strLen($this->title) > 0) $form = $this->postTitle(); //Ouverture du form et table $form.= '<p>'."\n"; $form.= '<form method="'.$this->method.'" action="'.$this->action.'">'."\n"; $form.= " ".'<table>'."\n"; //On affiche chaque Input du tableau dans une ligne du tableau foreach($this->formArray as $controlValues) { $form.= " ".'<tr>'."\n"; $form.= " ".'<td align="right"'; //Si le champ est en erreur on le signal en modifiant son style $form.= ( $controlValues['error']==1 ? ' style="color:red;"' : ''); $form.= '>'; $form.= '<label name="'.$controlValues['name'].'">'.$controlValues['label'].'</label></td>'."\n"; $form.= " ".'<td>'.$this->getHTMLinput($controlValues).'</td>'."\n"; $form.= " ".'</tr>'."\n"; } $form.= " ".'<tr>'."\n"; $form.= " ".'<td align="right" colspan="2">'.$this->getHTMLinput(array('type' => 'submit', 'value' => 'ok') ); //bouton reset if ($this->resetButton) $form.= " ".$this->getHTMLinput(array('type' => 'reset', 'value' => 'Reset') ); $form.= '</td>'."\n"; $form.= " ".'</tr>'."\n"; //On ferme le table et le form $form.= " ".'</table>'."\n"; $form.= '</form>'."\n"; $form.= '</p>'."\n"; return $form; } //METHODES PUBLICS================================================================================ //On remplit le tableau de form //Un controle = une ligne dans le tableau Public function addInput ($label, $type, $name, $size, $maxlength, $value, $required) { //On récupère le nom de l'input et on l'encrypte $finalName = $this->getInputName(trim($name), trim($type), trim($required)); $encode = new encode(self::_KEY); $EncryptedName = $encode->crypte($finalName); $this->formArray[$this->indice]['name'] = $EncryptedName; $this->formArray[$this->indice]['label'] = trim($label); $this->formArray[$this->indice]['type'] = trim($type); $this->formArray[$this->indice]['EncryptedName'] = trim($EncryptedName); $this->formArray[$this->indice]['size'] = trim($size); $this->formArray[$this->indice]['maxlength'] = trim($maxlength); if(!isSet($_POST['submit'])) $this->formArray[$this->indice]['value'] = trim($value); $this->formArray[$this->indice]['required'] = trim($required); $this->formArray[$this->indice]['error'] = 0; $this->indice++; } public function setTitle($title){ $this->title=$title; } //Ajouter un boutton de reset sur le formulaire Public function addResetButton (){ $this->resetButton = TRUE; } public function loadControlValue($value, $index){ $this->formArray[$index]['value'] = $value; } //Permet de mettre un control en erreur lors de sa vérification public function setControlError($key){ $this->formArray[$key]['error'] = 1; } //On efface la valeur saisie si celle ci ne correspond pas au type demandé public function delControlValue($key){ $this->formArray[$key]['value'] = ''; } //METHODES PRIVATE================================================================================ //Ajout du titre private function postTitle (){ $title = '<p>'."\n"; $title.= '<h2>'.$this->title.'</h2>'."\n"; $title.= '</p>'."\n"; return $title; } //Génère un nom qui sera hashé pour en déduire le type, nom et si le champ est obligatoire private function getInputName ($name, $type, $required){ if ( $required == TRUE or $required == 1) $finalRequired = 1; else $finalRequired = 0; $finalName = $this->getTypePrfx ($type).'|'; $finalName.= $name.'|'; $finalName.= $finalRequired; return $finalName; } //Récupération du préfixe qui sera ajouté au nom de l'input private function getTypePrfx ($type){ switch($type){ case 'text' : return 'txt'; break; case 'number' : return 'nbr'; break; case 'password' : return 'pwd'; break; case 'hidden' : return 'hdn'; break; case 'date' : return 'dt'; break; case 'checkbox' : return 'chk'; break; case 'radio' : return 'rdo'; break; default : return FALSE; } } private function getHTMLInput($inputTab){ /*On prépare le <input type=... pour certains types Pour d'autres type de champs on sait que l'attribut name est obligatoire Même si pour reset et submit le name n'est pas obligatoire, je le rends obligatoire */ switch ($inputTab['type']){ case 'text' : $HTMLinput = $this->getHTMLtype('text'); $HTMLinput.= ' '.$this->getHTMLname($inputTab['name']); $HTMLinput.= ' '.$this->getHTMLvalue($inputTab['value']); $HTMLinput.= ' '.$this->getHTMLsize ($inputTab['size']); $HTMLinput.= ' '.$this->getHTMLmaxlength ($inputTab['maxlength']); return $HTMLinput.' />'; break; case 'password' : $HTMLinput = $this->getHTMLtype('text'); $HTMLinput.= ' '.$this->getHTMLname($inputTab['name']); $HTMLinput.= ' '.$this->getHTMLsize ($inputTab['size']); $HTMLinput.= ' '.$this->getHTMLmaxlength ($inputTab['maxlength']); return $HTMLinput.' />'; break; case 'number' : $HTMLinput = $this->getHTMLtype('text'); $HTMLinput.= ' '.$this->getHTMLname($inputTab['name']); $HTMLinput.= ' '.$this->getHTMLvalue($inputTab['value']); $HTMLinput.= ' '.$this->getHTMLsize ($inputTab['size']); $HTMLinput.= ' '.$this->getHTMLmaxlength ($inputTab['maxlength']); return $HTMLinput.' />'; break; case 'checkbox' : return; break; case 'textarea' : return; break; case 'select' : return; break; case 'radio' : return; break; case 'submit' : $HTMLinput = $this->getHTMLtype('submit'); $HTMLinput.= ' '.$this->getHTMLname($this->submitName); $HTMLinput.= ' '.$this->getHTMLvalue($inputTab['value']); return $HTMLinput.' />'; break; case 'file' : return; break; case 'reset' : $HTMLinput = $this->getHTMLtype('reset'); $HTMLinput.= ' '.$this->getHTMLvalue($inputTab['value']); return $HTMLinput.' />'; break; case 'button' : return; break; case 'hidden' : return; break; case 'file' : return; break; default : return; break; } } //Création des attributs des controls des forms---------------------------------- private function getHTMLtype($type){ return '<input type="'.$type.'"'; } private function getHTMLname($name){ return 'name="'.$name.'"'; } private function getHTMLvalue($value){ return (strLen($value)>0 ? 'value="'.$value.'"' : '' ); } private function getHTMLsize($size){ return (strLen($size)>0 ? 'size="'.$size.'"' : '' ); } private function getHTMLmaxlength($maxlength){ return (strLen($maxlength)>0 ? 'maxlength="'.$maxlength.'"' : '' ); } //------------------------------------------------------------------------------- } ?>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 <?php include("encode.class.php5"); class checkFormValue{ private $tabValues; private $decryptKey; function __construct ($tabValues, $decryptKey){ $this->tabValues = $tabValues; $this->decryptKey = $decryptKey; } //On balaye et on decrypte les données envoyées Public function checkFormInput ($formArray) { //On instancie la classe de cryptage pour appeler la méthode de décryptage $decrypt = new encode($this->decryptKey); $i=0; foreach ($this->tabValues as $key=>$value) { //On décrypte le nom du champ $tab = explode('|', $decrypt->decrypte($key)); //3 Etapes sont à vérifier pour valider le formulaire //Pour chaque champs saisie on vérifie : // - que les champs obligatoires soient saisis // - que le type de champ correspond bien au type déclaré // - que sa taille maximum est pas dépassée //Vérification des champs obligatoires if ($tab[2] == 1 && (!isSet($value) || (strlen(trim($value))==0))){ //Si on a déjà trouvé un champ obligatoire non rempli //on ne réaffiche pas le message d'erreur if ($requiredError != 1 ) echo $this->getHTLMerrorMessage($this->getErrorMessage('required')); $formArray->setControlError($i); $requiredError = 1; } //Vérification de la concordence entre le type et la saisie //Attention au submit(à ne pas vérifier) if (!$this->checkInputValue ($tab[0], $value) && $key != 'submit'){ echo $this->getHTLMerrorMessage($this->getErrorMessage($tab[0])); $formArray->setControlError($i); $formArray->delControlValue($i); $requiredError = 1;; } $i++; } return ( $requiredError == 1 ? FALSE : TRUE); } //On décompose la valeur pour extraire private function checkInputValue ($typeValue, $value){ //Si la valeur est vide pas la peine de vérifier son type if (!isSet($value) or strLen(trim($value)) == 0) return TRUE; switch($typeValue){ case 'txt' : //Pas de vérification sur le texte car saisie libre ! break; case 'nbr' : return (is_numeric($value) ? TRUE : FALSE); break; case 'pwd' : //Pas de vérification sur le texte car saisie libre ! break; case 'hdn' : //Pas de vérification sur le texte car saisie libre ! break; case 'dt' : break; case 'chk' : break; case 'rdo' : break; default : return FALSE; } return TRUE; } private function getErrorMessage ($typeValue){ switch($typeValue){ case 'txt' : return 'Veuillez sa'; break; case 'nbr' : return 'Veuillez saisir un nombre où c\'est demandé'; break; case 'pwd' : break; case 'hdn' : break; case 'dt' : break; case 'chk' : break; case 'rdo' : break; //Champ obligatoire case 'required' : return 'Veuillez saisir tous les champs obligatoires'; break; default : return 'Il n\'y a pas de message d\'erreur pour le type de champ '.$typeValue; } } private function getHTLMerrorMessage($errorMessage) { $HTML = '<p>'."\n"; $HTML.= " ".'<font style="color:red;">'.$errorMessage.'</font>'."\n"; $HTML.= '</p>'."\n"; return $HTML; } } ?>
Partager