1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\HTMLForm; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Example of FormModel implementation. |
7
|
|
|
*/ |
8
|
|
|
class FormModelCheckboxMultiple extends FormModel |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Constructor |
12
|
|
|
*/ |
13
|
|
|
public function __construct() |
14
|
|
|
{ |
15
|
|
|
parent::__construct( |
16
|
|
|
[ |
17
|
|
|
"id" => __CLASS__, |
18
|
|
|
], |
19
|
|
|
[ |
20
|
|
|
"items" => [ |
21
|
|
|
"type" => "checkbox-multiple", |
22
|
|
|
"values" => ["tomato", "potato", "apple", "pear", "banana"], |
23
|
|
|
"checked" => ["potato", "pear"], |
24
|
|
|
], |
25
|
|
|
"submit" => [ |
26
|
|
|
"type" => "submit", |
27
|
|
|
"callback" => [$this, "callbackSubmit"], |
28
|
|
|
], |
29
|
|
|
"submit-fail" => [ |
30
|
|
|
"type" => "submit", |
31
|
|
|
"callback" => [$this, "callbackSubmitFail"], |
32
|
|
|
], |
33
|
|
|
] |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Callback for submit-button. |
41
|
|
|
*/ |
42
|
|
View Code Duplication |
public function callbackSubmit() |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$this->AddOutput("<p>#callbackSubmit()</p>"); |
45
|
|
|
|
46
|
|
|
// Get the selected items as an array |
47
|
|
|
$items = $this->value("items"); |
48
|
|
|
$itemsAsString = implode(", ", $items); |
49
|
|
|
$this->AddOutput("<p>Selected items are: '$itemsAsString'."); |
50
|
|
|
|
51
|
|
|
$this->AddOutput("<pre>" . print_r($_POST, 1) . "</pre>"); |
52
|
|
|
$this->AddOutput("<pre>" . print_r($this["items"], 1) . "</pre>"); |
53
|
|
|
$this->saveInSession = true; |
|
|
|
|
54
|
|
|
|
55
|
|
|
return true; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Callback for submit-button. |
62
|
|
|
*/ |
63
|
|
|
public function callbackSubmitFail() |
64
|
|
|
{ |
65
|
|
|
$this->AddOutput("<p>#callbackSubmitFail()</p>"); |
66
|
|
|
$this->AddOutput("<p>Form was submitted but I failed to process/save/validate it</p>"); |
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.