1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Fiv\Form; |
5
|
|
|
|
6
|
|
|
use Fiv\Form\Element; |
7
|
|
|
use Fiv\Form\Element\Checkbox; |
|
|
|
|
8
|
|
|
use Fiv\Form\Element\CheckboxList; |
|
|
|
|
9
|
|
|
use Fiv\Form\Element\Submit; |
10
|
|
|
use Fiv\Form\Element\TextArea; |
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author Ivan Shcherbak <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class Form extends Element\Html { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var null|string |
19
|
|
|
*/ |
20
|
|
|
protected $uid = null; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var boolean|null |
24
|
|
|
*/ |
25
|
|
|
protected $validationResult = null; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $errorList = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array|null |
34
|
|
|
*/ |
35
|
|
|
protected $data = null; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var bool |
39
|
|
|
*/ |
40
|
|
|
protected $isSubmitted = false; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var Element\BaseElement[] |
44
|
|
|
*/ |
45
|
|
|
protected $elements = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Default form attributes |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected $attributes = [ |
53
|
|
|
'method' => 'post', |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param $name |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
8 |
|
public function setName($name) { |
62
|
8 |
|
$this->uid = $name; |
63
|
8 |
|
$this->attributes['name'] = $name; |
64
|
8 |
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return array |
70
|
|
|
* @throws \Exception |
71
|
|
|
*/ |
72
|
1 |
|
public function getData() { |
73
|
1 |
|
if ($this->data === null) { |
74
|
|
|
throw new \Exception('Data does not exist!'); |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
return $this->data; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param array|\Iterator $data |
83
|
|
|
* @throws \Exception |
84
|
|
|
*/ |
85
|
16 |
|
public function setData($data) { |
86
|
16 |
|
if ($data instanceof \Iterator) { |
87
|
|
|
$data = iterator_to_array($data); |
88
|
|
|
} |
89
|
|
|
|
90
|
16 |
|
if (!is_array($data)) { |
91
|
1 |
|
throw new \Exception('Data should be an array'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
15 |
|
$this->cleanValidationFlag(); |
96
|
|
|
|
97
|
15 |
|
$formData = []; |
98
|
15 |
|
foreach ($this->elements as $element) { |
99
|
14 |
|
$name = $element->getName(); |
100
|
|
|
|
101
|
14 |
|
if ($element instanceof Checkbox) { |
102
|
2 |
|
$data[$name] = array_key_exists($name, $data) ? 1 : 0; |
103
|
12 |
|
} elseif ($element instanceof CheckboxList) { |
104
|
|
|
$data[$name] = isset($data[$name]) ? $data[$name] : []; |
105
|
|
|
} |
106
|
|
|
|
107
|
14 |
|
if (array_key_exists($name, $data)) { |
108
|
13 |
|
$element->setValue($data[$name]); |
109
|
14 |
|
$formData[$name] = $element->getValue(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
15 |
|
$this->isSubmitted = isset($data[$this->getUid()]); |
115
|
15 |
|
$this->data = $formData; |
116
|
15 |
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
4 |
|
public function getMethod() { |
123
|
4 |
|
if (!empty($this->attributes['method'])) { |
124
|
4 |
|
return strtolower($this->attributes['method']); |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
return null; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $method |
133
|
|
|
* @return $this |
134
|
|
|
*/ |
135
|
2 |
|
public function setMethod($method) { |
136
|
2 |
|
$this->attributes['method'] = $method; |
137
|
|
|
|
138
|
2 |
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* |
144
|
|
|
*/ |
145
|
24 |
|
protected function cleanValidationFlag() { |
146
|
24 |
|
$this->errorList = []; |
147
|
24 |
|
$this->validationResult = null; |
148
|
24 |
|
} |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Check if form is submitted and all elements are valid |
153
|
|
|
* |
154
|
|
|
* @return boolean |
155
|
|
|
*/ |
156
|
11 |
|
public function isValid() { |
157
|
11 |
|
if ($this->validationResult !== null) { |
158
|
1 |
|
return $this->validationResult; |
159
|
|
|
} |
160
|
|
|
|
161
|
11 |
|
if (!$this->isSubmitted()) { |
162
|
2 |
|
return false; |
163
|
|
|
} |
164
|
|
|
|
165
|
10 |
|
$this->validationResult = true; |
166
|
10 |
|
foreach ($this->elements as $element) { |
167
|
10 |
|
if (!$element->isValid()) { |
168
|
6 |
|
$this->errorList = array_merge($this->errorList, $element->getValidatorsErrors()); |
169
|
10 |
|
$this->validationResult = false; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
10 |
|
return $this->validationResult; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return array |
179
|
|
|
*/ |
180
|
2 |
|
public function getErrors() { |
181
|
2 |
|
return $this->errorList; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param string $error |
187
|
|
|
* @return $this |
188
|
|
|
*/ |
189
|
1 |
|
protected function addError($error) { |
190
|
1 |
|
if (!is_string($error)) { |
191
|
|
|
throw new \InvalidArgumentException('Error should be a string, ' . gettype($error) . ' given.'); |
192
|
|
|
} |
193
|
1 |
|
$this->validationResult = false; |
194
|
1 |
|
$this->errorList[] = $error; |
195
|
1 |
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Check if form is submitted |
201
|
|
|
* |
202
|
|
|
* @return bool |
203
|
|
|
*/ |
204
|
13 |
|
public function isSubmitted() { |
205
|
13 |
|
return $this->isSubmitted; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Return unique id of form |
211
|
|
|
* |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
19 |
|
public function getUid() { |
215
|
19 |
|
if (empty($this->uid)) { |
216
|
15 |
|
$this->uid = md5(get_called_class()); |
217
|
|
|
} |
218
|
|
|
|
219
|
19 |
|
return $this->uid; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @return Element\BaseElement[] |
225
|
|
|
*/ |
226
|
5 |
|
public function getElements() { |
227
|
5 |
|
return $this->elements; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @param string $name |
233
|
|
|
* @param string|null $text |
234
|
|
|
* @return \Fiv\Form\Element\Input |
235
|
|
|
*/ |
236
|
10 |
|
public function input($name, $text = null) { |
237
|
10 |
|
$input = new Element\Input(); |
238
|
10 |
|
$input->setName($name); |
239
|
10 |
|
$input->setText($text); |
240
|
10 |
|
$this->addElement($input); |
241
|
10 |
|
return $input; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @param string $name |
247
|
|
|
* @param string|null $text |
248
|
|
|
* @return \Fiv\Form\Element\Input |
249
|
|
|
*/ |
250
|
|
|
public function password($name, $text = null) { |
251
|
|
|
$input = new Element\Password(); |
252
|
|
|
$input->setName($name); |
253
|
|
|
$input->setText($text); |
254
|
|
|
$this->addElement($input); |
255
|
|
|
return $input; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @param string $name |
261
|
|
|
* @param null $text |
262
|
|
|
* @return Select |
263
|
|
|
*/ |
264
|
|
|
public function select($name, $text = null) { |
265
|
|
|
$select = new Element\Select(); |
266
|
|
|
$select->setName($name); |
267
|
|
|
$select->setText($text); |
268
|
|
|
$this->addElement($select); |
269
|
|
|
return $select; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @param string $name |
275
|
|
|
* @param string $text |
276
|
|
|
* @return RadioList |
277
|
|
|
*/ |
278
|
|
|
public function radioList($name, $text = null) { |
279
|
|
|
$radio = new Element\RadioList(); |
280
|
|
|
$radio->setName($name); |
281
|
|
|
$radio->setText($text); |
282
|
|
|
$this->addElement($radio); |
283
|
|
|
return $radio; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @param string $name |
289
|
|
|
* @param null $text |
290
|
|
|
* @return TextArea |
291
|
|
|
*/ |
292
|
4 |
|
public function textarea($name, $text = null) { |
293
|
4 |
|
$input = new TextArea(); |
294
|
4 |
|
$input->setName($name); |
295
|
4 |
|
$input->setText($text); |
296
|
4 |
|
$this->addElement($input); |
297
|
4 |
|
return $input; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* ``` |
303
|
|
|
* $form->hidden('key', md5($this-user->id . HASH_A); |
304
|
|
|
* ``` |
305
|
|
|
* @param string $name |
306
|
|
|
* @param null $value |
307
|
|
|
* @return \Fiv\Form\Element\Input |
308
|
|
|
*/ |
309
|
3 |
|
public function hidden($name, $value = null) { |
310
|
3 |
|
$hidden = new \Fiv\Form\Element\Input(); |
311
|
3 |
|
$hidden->setType('hidden'); |
312
|
3 |
|
$hidden->setName($name); |
313
|
3 |
|
$hidden->setValue($value); |
314
|
3 |
|
$this->addElement($hidden); |
315
|
3 |
|
return $hidden; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* ``` |
321
|
|
|
* $form->submit('register', 'зареєструватись'); |
322
|
|
|
* ``` |
323
|
|
|
* @param string $name |
324
|
|
|
* @param null $value |
325
|
|
|
* @return Submit |
326
|
|
|
*/ |
327
|
1 |
|
public function submit($name, $value = null) { |
328
|
1 |
|
$input = new Submit(); |
329
|
1 |
|
$input->setName($name); |
330
|
1 |
|
$input->setValue($value); |
331
|
1 |
|
$this->addElement($input); |
332
|
1 |
|
return $input; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* ``` |
338
|
|
|
* $form->checkbox('subscribe', 'Підписка на новини'); |
339
|
|
|
* ``` |
340
|
|
|
* @param string $name |
341
|
|
|
* @param string|null $label |
342
|
|
|
* @return Checkbox |
343
|
|
|
*/ |
344
|
2 |
|
public function checkbox($name, $label = null) { |
345
|
2 |
|
$checkbox = new Checkbox(); |
346
|
2 |
|
$checkbox->setName($name); |
347
|
2 |
|
$checkbox->setLabel($label); |
348
|
2 |
|
$this->addElement($checkbox); |
349
|
2 |
|
return $checkbox; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* @param string $name |
355
|
|
|
* @param null $text |
356
|
|
|
* @return CheckboxList |
357
|
|
|
*/ |
358
|
|
|
public function checkboxList($name, $text = null) { |
359
|
|
|
$checkbox = new CheckboxList(); |
360
|
|
|
$checkbox->setName($name); |
361
|
|
|
$checkbox->setText($text); |
362
|
|
|
$this->addElement($checkbox); |
363
|
|
|
return $checkbox; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Attach element to this form. Overwrite element with same name |
369
|
|
|
* |
370
|
|
|
* @param Element\BaseElement $element |
371
|
|
|
* @return $this |
372
|
|
|
*/ |
373
|
2 |
|
public function setElement(Element\BaseElement $element) { |
374
|
2 |
|
$this->cleanValidationFlag(); |
375
|
2 |
|
$this->elements[$element->getName()] = $element; |
376
|
2 |
|
return $this; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* @param Element\BaseElement $element |
382
|
|
|
* @return $this |
383
|
|
|
* @throws \Exception |
384
|
|
|
*/ |
385
|
21 |
|
public function addElement(Element\BaseElement $element) { |
386
|
21 |
|
if (isset($this->elements[$element->getName()])) { |
387
|
1 |
|
throw new \Exception('Element with name ' . $element->getName() . ' is already added. Use setElement to overwrite it or change name'); |
388
|
|
|
} |
389
|
|
|
|
390
|
21 |
|
$this->cleanValidationFlag(); |
391
|
21 |
|
$this->elements[$element->getName()] = $element; |
392
|
21 |
|
return $this; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Render full form |
398
|
|
|
* |
399
|
|
|
* @return string |
400
|
|
|
*/ |
401
|
2 |
|
public function render() { |
402
|
2 |
|
return $this->renderStart() . $this->renderElements() . $this->renderEnd(); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* You can easy rewrite this method for custom design of your forms |
408
|
|
|
* |
409
|
|
|
* @return string |
410
|
|
|
*/ |
411
|
2 |
|
protected function renderElements() { |
412
|
2 |
|
$formHtml = '<dl>'; |
413
|
|
|
|
414
|
2 |
|
foreach ($this->elements as $element) { |
415
|
|
|
# skip hidden element |
416
|
1 |
|
if ($element instanceof Element\Input and $element->getType() === 'hidden') { |
417
|
|
|
continue; |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
$formHtml .= |
421
|
1 |
|
'<dt>' . $element->getText() . '</dt>' . |
422
|
1 |
|
'<dd>' . $element->render() . '</dd>'; |
423
|
|
|
} |
424
|
|
|
|
425
|
2 |
|
$formHtml .= '</dl>'; |
426
|
2 |
|
return $formHtml; |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* @return string |
432
|
|
|
*/ |
433
|
3 |
|
public function renderStart() { |
434
|
3 |
|
$hidden = new Element\Input(); |
435
|
3 |
|
$hidden->setType('hidden'); |
436
|
3 |
|
$hidden->addAttributes([ |
437
|
3 |
|
'name' => $this->getUid(), |
438
|
|
|
]); |
439
|
3 |
|
$hidden->setValue(1); |
440
|
|
|
|
441
|
|
|
|
442
|
|
|
# get default attribute |
443
|
3 |
|
$method = $this->getMethod(); |
444
|
3 |
|
$this->setAttribute('method', $method); |
445
|
|
|
|
446
|
3 |
|
$html = '<form ' . Element\Html::renderAttributes($this->getAttributes()) . '>'; |
447
|
3 |
|
$html .= $hidden->render(); |
448
|
|
|
|
449
|
|
|
# render hidden element |
450
|
3 |
|
foreach ($this->elements as $element) { |
451
|
2 |
|
if ($element instanceof Element\Input and $element->getType() === 'hidden') { |
452
|
2 |
|
$html .= $element->render(); |
453
|
|
|
} |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
|
457
|
3 |
|
return $html; |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
|
461
|
|
|
/** |
462
|
|
|
* @return string |
463
|
|
|
*/ |
464
|
3 |
|
public function renderEnd() { |
465
|
3 |
|
return '</form>'; |
466
|
|
|
} |
467
|
|
|
} |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: