1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\HTMLForm; |
4
|
|
|
|
5
|
|
|
use \Anax\DI\DIInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* A utility class to easy creating and handling of forms |
9
|
|
|
*/ |
10
|
|
|
class Form implements \ArrayAccess |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array $form settings for the form |
14
|
|
|
* @var array $elements all form elements |
15
|
|
|
* @var array $output messages to display together with the form |
16
|
|
|
* @var array $sessionKey key values for the session |
17
|
|
|
*/ |
18
|
|
|
public $form; |
19
|
|
|
public $elements; |
20
|
|
|
public $output; |
21
|
|
|
public $sessionKey; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var boolean $rememberValues remember values in the session. |
25
|
|
|
*/ |
26
|
|
|
public $rememberValues; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Anax\DI\DIInterface $di the DI service container. |
30
|
|
|
*/ |
31
|
|
|
protected $di; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Constructor injects with DI container. |
37
|
|
|
* |
38
|
|
|
* @param Anax\DI\DIInterface $di a service container |
39
|
|
|
*/ |
40
|
2 |
|
public function __construct(DIInterface $di) |
41
|
|
|
{ |
42
|
2 |
|
$this->di = $di; |
|
|
|
|
43
|
2 |
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Implementing ArrayAccess for this->elements |
49
|
|
|
*/ |
50
|
|
|
public function offsetSet($offset, $value) |
51
|
|
|
{ |
52
|
|
View Code Duplication |
if (is_null($offset)) { |
|
|
|
|
53
|
|
|
$this->elements[] = $value; |
54
|
|
|
} else { |
55
|
|
|
$this->elements[$offset] = $value; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function offsetExists($offset) |
60
|
|
|
{ |
61
|
|
|
return isset($this->elements[$offset]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function offsetUnset($offset) |
65
|
|
|
{ |
66
|
|
|
unset($this->elements[$offset]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function offsetGet($offset) |
70
|
|
|
{ |
71
|
|
|
return isset($this->elements[$offset]) |
72
|
|
|
? $this->elements[$offset] |
73
|
|
|
: null; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Add a form element |
80
|
|
|
* |
81
|
|
|
* @param array $form details for the form |
82
|
|
|
* @param array $elements all the elements |
83
|
|
|
* |
84
|
|
|
* @return $this |
85
|
|
|
*/ |
86
|
2 |
|
public function create($form = [], $elements = []) |
87
|
|
|
{ |
88
|
|
|
$defaults = [ |
89
|
|
|
// Always have a id for the form |
90
|
2 |
|
"id" => "anax/htmlform", |
91
|
2 |
|
]; |
92
|
2 |
|
$this->form = array_merge($defaults, $form); |
93
|
|
|
|
94
|
2 |
|
$this->elements = []; |
95
|
2 |
|
if (!empty($elements)) { |
96
|
|
|
foreach ($elements as $key => $element) { |
97
|
|
|
$this->elements[$key] = FormElement::Create($key, $element); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
2 |
|
$this->output = []; |
102
|
|
|
|
103
|
|
|
// Setting keys used in the session |
104
|
2 |
|
$generalKey = "anax/htmlform-" . $this->form["id"] . "#"; |
105
|
2 |
|
$this->sessionKey = [ |
106
|
2 |
|
"save" => $generalKey . "save", |
107
|
2 |
|
"output" => $generalKey . "output", |
108
|
2 |
|
"failed" => $generalKey . "failed", |
109
|
2 |
|
"remember" => $generalKey . "remember", |
110
|
|
|
]; |
111
|
|
|
|
112
|
2 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Add a form element |
119
|
|
|
* |
120
|
|
|
* @param FormElement $element the formelement to add. |
121
|
|
|
* |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
|
|
public function addElement($element) |
125
|
|
|
{ |
126
|
|
|
$name = $element; |
127
|
|
|
if (isset($this->elements[$name])) { |
128
|
|
|
throw new Exception("Form element '$name' already exists, do not add it twice."); |
129
|
|
|
} |
130
|
|
|
$this[$element['name']] = $name; |
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get a form element |
138
|
|
|
* |
139
|
|
|
* @param string $name the name of the element. |
140
|
|
|
* |
141
|
|
|
* @return \Anax\HTMLForm\FormElement |
142
|
|
|
*/ |
143
|
|
View Code Duplication |
public function getElement($name) |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
if (!isset($this->elements[$name])) { |
146
|
|
|
throw new Exception("Form element '$name' is not found."); |
147
|
|
|
} |
148
|
|
|
return $this->elements[$name]; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Remove an form element |
155
|
|
|
* |
156
|
|
|
* @param string $name the name of the element. |
157
|
|
|
* |
158
|
|
|
* @return $this |
159
|
|
|
*/ |
160
|
|
View Code Duplication |
public function removeElement($name) |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
if (!isset($this->elements[$name])) { |
163
|
|
|
throw new Exception("Form element '$name' is not found."); |
164
|
|
|
} |
165
|
|
|
unset($this->elements[$name]); |
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
|
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Set validation to a form element |
173
|
|
|
* |
174
|
|
|
* @param string $element the name of the formelement to add validation rules to. |
175
|
|
|
* @param array $rules array of validation rules. |
176
|
|
|
* |
177
|
|
|
* @return $this |
178
|
|
|
*/ |
179
|
|
|
public function setValidation($element, $rules) |
180
|
|
|
{ |
181
|
|
|
$this[$element]['validation'] = $rules; |
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Add output to display to the user what happened whith the form. |
189
|
|
|
* |
190
|
|
|
* @param string $str the string to add as output. |
191
|
|
|
* |
192
|
|
|
* @return $this. |
|
|
|
|
193
|
|
|
*/ |
194
|
|
|
public function addOutput($str) |
195
|
|
|
{ |
196
|
|
|
$key = $this->sessionKey["output"]; |
197
|
|
|
$session = $this->di->get("session"); |
198
|
|
|
$output = $session->get($key); |
199
|
|
|
$output = $output ? "$output $str" : $str; |
200
|
|
|
$session->set($key, $output); |
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
|
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Remember current values in session, useful for storing values of |
208
|
|
|
* current form when submitting it. |
209
|
|
|
* |
210
|
|
|
* @return $this. |
|
|
|
|
211
|
|
|
*/ |
212
|
|
|
public function rememberValues() |
213
|
|
|
{ |
214
|
|
|
$this->rememberValues = true; |
215
|
|
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
|
219
|
|
|
|
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Get value of a form element |
223
|
|
|
* |
224
|
|
|
* @param string $name the name of the formelement. |
225
|
|
|
* |
226
|
|
|
* @return mixed the value of the element. |
227
|
|
|
*/ |
228
|
|
|
public function value($name) |
229
|
|
|
{ |
230
|
|
|
return isset($this->elements[$name]) |
231
|
|
|
? $this->elements[$name]->value() |
232
|
|
|
: null; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
|
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Check if a element is checked |
239
|
|
|
* |
240
|
|
|
* @param string $name the name of the formelement. |
241
|
|
|
* |
242
|
|
|
* @return mixed the value of the element. |
243
|
|
|
*/ |
244
|
|
|
public function checked($name) |
245
|
|
|
{ |
246
|
|
|
return isset($this->elements[$name]) |
247
|
|
|
? $this->elements[$name]->checked() |
248
|
|
|
: null; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
|
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Return HTML for the form. |
255
|
|
|
* |
256
|
|
|
* @param array $options with options affecting the form output. |
257
|
|
|
* |
258
|
|
|
* @return string with HTML for the form. |
259
|
|
|
*/ |
260
|
2 |
|
public function getHTML($options = []) |
261
|
|
|
{ |
262
|
|
|
$defaults = [ |
263
|
|
|
// Only return the start of the form element |
264
|
2 |
|
'start' => false, |
265
|
|
|
|
266
|
|
|
// Layout all elements in one column |
267
|
2 |
|
'columns' => 1, |
268
|
|
|
|
269
|
|
|
// Layout consequtive buttons as one element wrapped in <p> |
270
|
2 |
|
'use_buttonbar' => true, |
271
|
|
|
|
272
|
|
|
// Wrap fields within <fieldset> |
273
|
2 |
|
'use_fieldset' => true, |
274
|
|
|
|
275
|
|
|
// Use legend for fieldset |
276
|
2 |
|
'legend' => isset($this->form['legend']) ? |
277
|
2 |
|
$this->form['legend'] |
278
|
2 |
|
: null, |
279
|
2 |
|
]; |
280
|
2 |
|
$options = array_merge($defaults, $options); |
281
|
|
|
|
282
|
2 |
|
$form = array_merge($this->form, $options); |
283
|
2 |
|
$id = isset($form['id']) ? " id='{$form['id']}'" : null; |
284
|
2 |
|
$class = isset($form['class']) ? " class='{$form['class']}'" : null; |
285
|
2 |
|
$name = isset($form['name']) ? " name='{$form['name']}'" : null; |
286
|
2 |
|
$action = isset($form['action']) ? " action='{$form['action']}'" : null; |
287
|
2 |
|
$method = isset($form['method']) ? " method='{$form['method']}'" : " method='post'"; |
288
|
2 |
|
$enctype = isset($form['enctype']) ? " enctype='{$form['enctype']}'" : null; |
289
|
2 |
|
$cformId = isset($form['id']) ? "{$form['id']}" : null; |
290
|
|
|
|
291
|
2 |
|
if ($options['start']) { |
292
|
|
|
return "<form{$id}{$class}{$name}{$action}{$method}>\n"; |
293
|
|
|
} |
294
|
|
|
|
295
|
2 |
|
$fieldsetStart = '<fieldset>'; |
296
|
2 |
|
$legend = null; |
297
|
2 |
|
$fieldsetEnd = '</fieldset>'; |
298
|
2 |
|
if (!$options['use_fieldset']) { |
299
|
|
|
$fieldsetStart = $fieldsetEnd = null; |
300
|
|
|
} |
301
|
|
|
|
302
|
2 |
|
if ($options['use_fieldset'] && $options['legend']) { |
303
|
|
|
$legend = "<legend>{$options['legend']}</legend>"; |
304
|
|
|
} |
305
|
|
|
|
306
|
2 |
|
$elementsArray = $this->GetHTMLForElements($options); |
307
|
2 |
|
$elements = $this->GetHTMLLayoutForElements($elementsArray, $options); |
308
|
2 |
|
$output = $this->GetOutput(); |
309
|
|
|
|
310
|
|
|
$html = <<< EOD |
311
|
2 |
|
\n<form{$id}{$class}{$name}{$action}{$method}{$enctype}> |
312
|
2 |
|
<input type="hidden" name="anax/htmlform-id" value="$cformId" /> |
313
|
2 |
|
{$fieldsetStart} |
314
|
2 |
|
{$legend} |
315
|
2 |
|
{$elements} |
316
|
2 |
|
{$output} |
317
|
2 |
|
{$fieldsetEnd} |
318
|
2 |
|
</form>\n |
319
|
2 |
|
EOD; |
320
|
|
|
|
321
|
2 |
|
return $html; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
|
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Return HTML for the elements |
328
|
|
|
* |
329
|
|
|
* @param array $options with options affecting the form output. |
330
|
|
|
* |
331
|
|
|
* @return array with HTML for the formelements. |
332
|
|
|
*/ |
333
|
2 |
|
public function getHTMLForElements($options = []) |
334
|
|
|
{ |
335
|
|
|
$defaults = [ |
336
|
2 |
|
'use_buttonbar' => true, |
337
|
2 |
|
]; |
338
|
2 |
|
$options = array_merge($defaults, $options); |
339
|
|
|
|
340
|
2 |
|
$elements = array(); |
341
|
2 |
|
reset($this->elements); |
342
|
2 |
|
while (list($key, $element) = each($this->elements)) { |
|
|
|
|
343
|
|
|
if (in_array($element['type'], array('submit', 'reset', 'button')) |
344
|
|
|
&& $options['use_buttonbar'] |
345
|
|
|
) { |
346
|
|
|
// Create a buttonbar |
347
|
|
|
$name = 'buttonbar'; |
348
|
|
|
$html = "<p class='buttonbar'>\n" . $element->GetHTML() . ' '; |
349
|
|
|
|
350
|
|
|
// Get all following submits (and buttons) |
351
|
|
|
while (list($key, $element) = each($this->elements)) { |
|
|
|
|
352
|
|
|
if (in_array($element['type'], array('submit', 'reset', 'button'))) { |
353
|
|
|
$html .= $element->GetHTML(); |
354
|
|
|
} else { |
355
|
|
|
prev($this->elements); |
356
|
|
|
break; |
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
$html .= "\n</p>"; |
360
|
|
|
} else { |
361
|
|
|
// Just add the element |
362
|
|
|
$name = $element['name']; |
363
|
|
|
$html = $element->GetHTML(); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
$elements[] = array('name'=>$name, 'html'=> $html); |
367
|
|
|
} |
368
|
|
|
|
369
|
2 |
|
return $elements; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
|
373
|
|
|
|
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Place the elements according to a layout and return the HTML |
377
|
|
|
* |
378
|
|
|
* @param array $elements as returned from GetHTMLForElements(). |
379
|
|
|
* @param array $options with options affecting the layout. |
380
|
|
|
* |
381
|
|
|
* @return array with HTML for the formelements. |
382
|
|
|
*/ |
383
|
2 |
|
public function getHTMLLayoutForElements($elements, $options = []) |
384
|
|
|
{ |
385
|
|
|
$defaults = [ |
386
|
2 |
|
'columns' => 1, |
387
|
2 |
|
'wrap_at_element' => false, // Wraps column in equal size or at the set number of elements |
388
|
2 |
|
]; |
389
|
2 |
|
$options = array_merge($defaults, $options); |
390
|
|
|
|
391
|
2 |
|
$html = null; |
392
|
2 |
|
if ($options['columns'] === 1) { |
393
|
2 |
|
foreach ($elements as $element) { |
394
|
|
|
$html .= $element['html']; |
395
|
2 |
|
} |
396
|
2 |
|
} elseif ($options['columns'] === 2) { |
397
|
|
|
$buttonbar = null; |
398
|
|
|
$col1 = null; |
399
|
|
|
$col2 = null; |
400
|
|
|
|
401
|
|
|
$end = end($elements); |
402
|
|
|
if ($end['name'] == 'buttonbar') { |
403
|
|
|
$end = array_pop($elements); |
404
|
|
|
$buttonbar = "<div class='cform-buttonbar'>\n{$end['html']}</div>\n"; |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
$size = count($elements); |
408
|
|
|
$wrapAt = $options['wrap_at_element'] ? $options['wrap_at_element'] : round($size/2); |
409
|
|
|
for ($i=0; $i<$size; $i++) { |
410
|
|
|
if ($i < $wrapAt) { |
411
|
|
|
$col1 .= $elements[$i]['html']; |
412
|
|
|
} else { |
413
|
|
|
$col2 .= $elements[$i]['html']; |
414
|
|
|
} |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
$html = <<<EOD |
418
|
|
|
<div class='cform-columns-2'> |
419
|
|
|
<div class='cform-column-1'> |
420
|
|
|
{$col1} |
421
|
|
|
</div> |
422
|
|
|
<div class='cform-column-2'> |
423
|
|
|
{$col2} |
424
|
|
|
</div> |
425
|
|
|
{$buttonbar}</div> |
426
|
|
|
EOD; |
427
|
|
|
} |
428
|
|
|
|
429
|
2 |
|
return $html; |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
|
433
|
|
|
|
434
|
|
|
/** |
435
|
|
|
* Get an array with all elements that failed validation together with their id and validation message. |
436
|
|
|
* |
437
|
|
|
* @return array with elements that failed validation. |
438
|
|
|
*/ |
439
|
|
|
public function getValidationErrors() |
440
|
|
|
{ |
441
|
|
|
$errors = []; |
442
|
|
|
foreach ($this->elements as $name => $element) { |
443
|
|
|
if ($element['validation-pass'] === false) { |
444
|
|
|
$errors[$name] = [ |
445
|
|
|
'id' => $element->GetElementId(), |
446
|
|
|
'label' => $element['label'], |
447
|
|
|
'message' => implode(' ', $element['validation-messages']) |
448
|
|
|
]; |
449
|
|
|
} |
450
|
|
|
} |
451
|
|
|
return $errors; |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
|
455
|
|
|
|
456
|
|
|
/** |
457
|
|
|
* Get output messages as <output>. |
458
|
|
|
* |
459
|
|
|
* @return string|null with the complete <output> element or null if no output. |
460
|
|
|
*/ |
461
|
2 |
|
public function getOutput() |
462
|
|
|
{ |
463
|
2 |
|
return !empty($this->output) |
464
|
2 |
|
? "<output>{$this->output}</output>" |
465
|
2 |
|
: null; |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
|
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* Init all element with values from session, clear all and fill in with values from the session. |
472
|
|
|
* |
473
|
|
|
* @param array $values retrieved from session |
474
|
|
|
* |
475
|
|
|
* @return void |
476
|
|
|
*/ |
477
|
|
|
protected function initElements($values) |
478
|
|
|
{ |
479
|
|
|
// First clear all |
480
|
|
|
foreach ($this->elements as $key => $val) { |
481
|
|
|
// Do not reset value for buttons |
482
|
|
|
if (in_array($this[$key]['type'], array('submit', 'reset', 'button'))) { |
483
|
|
|
continue; |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
// Reset the value |
487
|
|
|
$this[$key]['value'] = null; |
488
|
|
|
|
489
|
|
|
// Checkboxes must be cleared |
490
|
|
|
if (isset($this[$key]['checked'])) { |
491
|
|
|
$this[$key]['checked'] = false; |
492
|
|
|
} |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
// Now build up all values from $values (session) |
496
|
|
|
foreach ($values as $key => $val) { |
497
|
|
|
// Take care of arrays as values (multiple-checkbox) |
498
|
|
|
if (isset($val['values'])) { |
499
|
|
|
$this[$key]['checked'] = $val['values']; |
500
|
|
|
//$this[$key]['values'] = $val['values']; |
501
|
|
|
} elseif (isset($val['value'])) { |
502
|
|
|
$this[$key]['value'] = $val['value']; |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
if ($this[$key]['type'] === 'checkbox') { |
506
|
|
|
$this[$key]['checked'] = true; |
507
|
|
|
} elseif ($this[$key]['type'] === 'radio') { |
508
|
|
|
$this[$key]['checked'] = $val['value']; |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
// Keep track on validation messages if set |
512
|
|
|
if (isset($val['validation-messages'])) { |
513
|
|
|
$this[$key]['validation-messages'] = $val['validation-messages']; |
514
|
|
|
$this[$key]['validation-pass'] = false; |
515
|
|
|
} |
516
|
|
|
} |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
|
520
|
|
|
|
521
|
|
|
/** |
522
|
|
|
* Check if a form was submitted and perform validation and call callbacks. |
523
|
|
|
* The form is stored in the session if validation or callback fails. The |
524
|
|
|
* page should then be redirected to the original form page, the form |
525
|
|
|
* will populate from the session and should be rendered again. |
526
|
|
|
* Form elements may remember their value if 'remember' is set and true. |
527
|
|
|
* |
528
|
|
|
* @param callable $callIfSuccess handler to call if function returns true. |
529
|
|
|
* @param callable $callIfFail handler to call if function returns true. |
530
|
|
|
* |
531
|
|
|
* @throws \Anax\HTMLForm\Exception |
532
|
|
|
* |
533
|
|
|
* @return boolean|null $callbackStatus if submitted&validates, false if |
534
|
|
|
* not validate, null if not submitted. |
535
|
|
|
* If submitted the callback function |
536
|
|
|
* will return the actual value which |
537
|
|
|
* should be true or false. |
538
|
|
|
*/ |
539
|
|
|
public function check($callIfSuccess = null, $callIfFail = null) |
540
|
|
|
{ |
541
|
|
|
$remember = null; |
542
|
|
|
$validates = null; |
|
|
|
|
543
|
|
|
$callbackStatus = null; |
544
|
|
|
$values = []; |
545
|
|
|
|
546
|
|
|
// Remember flash output messages in session |
547
|
|
|
$output = $this->sessionKey["output"]; |
548
|
|
|
$session = $this->di->get("session"); |
549
|
|
|
$this->output = $session->getOnce($output, []); |
550
|
|
|
|
551
|
|
|
// Check if this was a post request |
552
|
|
|
$requestMethod = $this->di->get("request")->getServer("REQUEST_METHOD"); |
553
|
|
|
if ($requestMethod !== "POST") { |
554
|
|
|
// Its not posted, but check if values should be used from session |
555
|
|
|
$failed = $this->sessionKey["failed"]; |
556
|
|
|
$remember = $this->sessionKey["remember"]; |
557
|
|
|
$save = $this->sessionKey["save"]; |
558
|
|
|
|
559
|
|
|
if ($session->has($failed)) { |
560
|
|
|
// Read form data from session if the previous post failed |
561
|
|
|
// during validation. |
562
|
|
|
$this->InitElements($session->getOnce($failed)); |
563
|
|
|
} elseif ($session->has($remember)) { |
564
|
|
|
// Read form data from session if some form elements should |
565
|
|
|
// be remembered |
566
|
|
|
foreach ($session->getOnce($remember) as $key => $val) { |
567
|
|
|
$this[$key]['value'] = $val['value']; |
568
|
|
|
} |
569
|
|
|
} elseif ($session->has($save)) { |
570
|
|
|
// Read form data from session, |
571
|
|
|
// useful during test where the original form is displayed |
572
|
|
|
// with its posted values |
573
|
|
|
$this->InitElements($session->getOnce($save)); |
574
|
|
|
} |
575
|
|
|
|
576
|
|
|
return null; |
577
|
|
|
} |
578
|
|
|
|
579
|
|
|
$request = $this->di->get("request"); |
580
|
|
|
$formid = $request->getPost("anax/htmlform-id"); |
581
|
|
|
// Check if its a form we are dealing with |
582
|
|
|
if (!$formid) { |
583
|
|
|
return null; |
584
|
|
|
} |
585
|
|
|
|
586
|
|
|
// Check if its this form that was posted |
587
|
|
|
if ($this->form["id"] !== $formid) { |
588
|
|
|
return null; |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
// This form was posted, process it |
592
|
|
|
$session->delete($this->sessionKey["failed"]); |
593
|
|
|
$validates = true; |
594
|
|
|
foreach ($this->elements as $element) { |
595
|
|
|
$elementName = $element['name']; |
596
|
|
|
$elementType = $element['type']; |
597
|
|
|
|
598
|
|
|
$postElement = $request->getPost($elementName); |
599
|
|
|
if ($postElement) { |
600
|
|
|
// The form element has a value set |
601
|
|
|
// Multiple choices comes in the form of an array |
602
|
|
|
if (is_array($postElement)) { |
603
|
|
|
$values[$elementName]['values'] = $element['checked'] = $postElement; |
604
|
|
|
} else { |
605
|
|
|
$values[$elementName]['value'] = $element['value'] = $postElement; |
606
|
|
|
} |
607
|
|
|
|
608
|
|
|
// If the element is a password, do not remember. |
609
|
|
|
if ($elementType === 'password') { |
610
|
|
|
$values[$elementName]['value'] = null; |
611
|
|
|
} |
612
|
|
|
|
613
|
|
|
// If the element is a checkbox, set its value of checked. |
614
|
|
|
if ($elementType === 'checkbox') { |
615
|
|
|
$element['checked'] = true; |
616
|
|
|
} |
617
|
|
|
|
618
|
|
|
// If the element is a radio, set the value to checked. |
619
|
|
|
if ($elementType === 'radio') { |
620
|
|
|
$element['checked'] = $element['value']; |
621
|
|
|
} |
622
|
|
|
|
623
|
|
|
// Do validation of form element |
624
|
|
View Code Duplication |
if (isset($element['validation'])) { |
|
|
|
|
625
|
|
|
$element['validation-pass'] = $element->Validate($element['validation'], $this); |
626
|
|
|
|
627
|
|
|
if ($element['validation-pass'] === false) { |
628
|
|
|
$values[$elementName] = [ |
629
|
|
|
'value' => $element['value'], |
630
|
|
|
'validation-messages' => $element['validation-messages'] |
631
|
|
|
]; |
632
|
|
|
$validates = false; |
633
|
|
|
} |
634
|
|
|
} |
635
|
|
|
|
636
|
|
|
// Hmmm.... Why did I need this remember thing? |
637
|
|
|
if (isset($element['remember']) |
638
|
|
|
&& $element['remember'] |
639
|
|
|
) { |
640
|
|
|
$values[$elementName] = ['value' => $element['value']]; |
641
|
|
|
$remember = true; |
642
|
|
|
} |
643
|
|
|
|
644
|
|
|
// Carry out the callback if the form element validates |
645
|
|
|
// Hmmm, what if the element with the callback is not the last element? |
646
|
|
|
if (isset($element['callback']) |
647
|
|
|
&& $validates |
648
|
|
|
) { |
649
|
|
|
if (isset($element['callback-args'])) { |
650
|
|
|
$callbackStatus = call_user_func_array( |
651
|
|
|
$element['callback'], |
652
|
|
|
array_merge([$this]), |
653
|
|
|
$element['callback-args'] |
654
|
|
|
); |
655
|
|
|
} else { |
656
|
|
|
$callbackStatus = call_user_func($element['callback'], $this); |
657
|
|
|
} |
658
|
|
|
} |
659
|
|
|
} else { |
660
|
|
|
// The form element has no value set |
661
|
|
|
|
662
|
|
|
// Set element to null, then we know it was not set. |
663
|
|
|
//$element['value'] = null; |
664
|
|
|
//echo $element['type'] . ':' . $elementName . ':' . $element['value'] . '<br>'; |
665
|
|
|
|
666
|
|
|
// If the element is a checkbox, clear its value of checked. |
667
|
|
|
if ($element['type'] === 'checkbox' |
668
|
|
|
|| $element['type'] === 'checkbox-multiple' |
669
|
|
|
) { |
670
|
|
|
$element['checked'] = false; |
671
|
|
|
} |
672
|
|
|
|
673
|
|
|
// Do validation even when the form element is not set? |
674
|
|
|
// Duplicate code, revise this section and move outside |
675
|
|
|
// this if-statement? |
676
|
|
View Code Duplication |
if (isset($element['validation'])) { |
|
|
|
|
677
|
|
|
$element['validation-pass'] = $element->Validate($element['validation'], $this); |
678
|
|
|
|
679
|
|
|
if ($element['validation-pass'] === false) { |
680
|
|
|
$values[$elementName] = [ |
681
|
|
|
'value' => $element['value'], 'validation-messages' => $element['validation-messages'] |
682
|
|
|
]; |
683
|
|
|
$validates = false; |
684
|
|
|
} |
685
|
|
|
} |
686
|
|
|
} |
687
|
|
|
} |
688
|
|
|
|
689
|
|
|
// Prepare if data should be stored in the session during redirects |
690
|
|
|
// Did form validation or the callback fail? |
691
|
|
|
if ($validates === false |
692
|
|
|
|| $callbackStatus === false |
693
|
|
|
) { |
694
|
|
|
$session->set($this->sessionKey["failed"], $values); |
695
|
|
|
} elseif ($remember) { |
696
|
|
|
// Hmmm, why do I want to use this |
697
|
|
|
$session->set($this->sessionKey["remember"], $values); |
698
|
|
|
} |
699
|
|
|
|
700
|
|
|
if ($this->rememberValues) { |
701
|
|
|
// Remember all posted values |
702
|
|
|
$session->set($this->sessionKey["save"], $values); |
703
|
|
|
} |
704
|
|
|
|
705
|
|
|
// Lets se what the return value should be |
706
|
|
|
$ret = $validates |
707
|
|
|
? $callbackStatus |
708
|
|
|
: $validates; |
709
|
|
|
|
710
|
|
|
|
711
|
|
|
if ($ret === true && isset($callIfSuccess)) { |
712
|
|
|
// Use callback for success, if defined |
713
|
|
|
if (!is_callable($callIfSuccess)) { |
714
|
|
|
throw new Exception("Form, success-method is not callable."); |
715
|
|
|
} |
716
|
|
|
call_user_func_array($callIfSuccess, [$this]); |
717
|
|
|
} elseif ($ret === false && isset($callIfFail)) { |
718
|
|
|
// Use callback for fail, if defined |
719
|
|
|
if (!is_callable($callIfFail)) { |
720
|
|
|
throw new Exception("Form, success-method is not callable."); |
721
|
|
|
} |
722
|
|
|
call_user_func_array($callIfFail, [$this]); |
723
|
|
|
} |
724
|
|
|
|
725
|
|
|
return $ret; |
726
|
|
|
} |
727
|
|
|
} |
728
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..