1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\HTMLForm; |
4
|
|
|
|
5
|
|
|
use Anax\DI\DIInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Example of FormModel implementation. |
9
|
|
|
*/ |
10
|
|
|
class FormModelElementsAll extends FormModel |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Constructor injects with DI container. |
14
|
|
|
* |
15
|
|
|
* @param Anax\DI\DIInterface $di a service container |
16
|
|
|
*/ |
17
|
|
|
public function __construct(DIInterface $di) |
18
|
|
|
{ |
19
|
|
|
parent::__construct($di); |
20
|
|
|
$this->form->create( |
21
|
|
|
[ |
22
|
|
|
"id" => __CLASS__, |
23
|
|
|
"legend" => "Legend", |
24
|
|
|
], |
25
|
|
|
[ |
26
|
|
|
"text" => [ |
27
|
|
|
"type" => "text", |
28
|
|
|
"description" => "Here you can place a description.", |
29
|
|
|
"placeholder" => "Here is a placeholder", |
30
|
|
|
], |
31
|
|
|
|
32
|
|
|
"password" => [ |
33
|
|
|
"type" => "password", |
34
|
|
|
"description" => "Here you can place a description.", |
35
|
|
|
"placeholder" => "Here is a placeholder", |
36
|
|
|
], |
37
|
|
|
|
38
|
|
|
"hidden" => [ |
39
|
|
|
"type" => "hidden", |
40
|
|
|
"value" => "secret value", |
41
|
|
|
], |
42
|
|
|
|
43
|
|
|
"file" => [ |
44
|
|
|
"type" => "file", |
45
|
|
|
"description" => "Here you can place a description.", |
46
|
|
|
], |
47
|
|
|
|
48
|
|
|
"textarea" => [ |
49
|
|
|
"type" => "textarea", |
50
|
|
|
"description" => "Here you can place a description.", |
51
|
|
|
"placeholder" => "Here is a placeholder", |
52
|
|
|
], |
53
|
|
|
|
54
|
|
|
"radio" => [ |
55
|
|
|
"type" => "radio", |
56
|
|
|
"label" => "What is your preferred choice of fruite?", |
57
|
|
|
"description" => "Here you can place a description.", |
58
|
|
|
"values" => [ |
59
|
|
|
"tomato", |
60
|
|
|
"potato", |
61
|
|
|
"apple", |
62
|
|
|
"pear", |
63
|
|
|
"banana" |
64
|
|
|
], |
65
|
|
|
"checked" => "potato", |
66
|
|
|
], |
67
|
|
|
|
68
|
|
|
"checkbox" => [ |
69
|
|
|
"type" => "checkbox", |
70
|
|
|
"description" => "Here you can place a description.", |
71
|
|
|
], |
72
|
|
|
|
73
|
|
|
"select" => [ |
74
|
|
|
"type" => "select", |
75
|
|
|
"label" => "Select your fruite:", |
76
|
|
|
"description" => "Here you can place a description.", |
77
|
|
|
"options" => [ |
78
|
|
|
"tomato" => "tomato", |
79
|
|
|
"potato" => "potato", |
80
|
|
|
"apple" => "apple", |
81
|
|
|
"pear" => "pear", |
82
|
|
|
"banana" => "banana", |
83
|
|
|
], |
84
|
|
|
"value" => "potato", |
85
|
|
|
], |
86
|
|
|
|
87
|
|
|
"selectm" => [ |
88
|
|
|
"type" => "select-multiple", |
89
|
|
|
"label" => "Select one or more fruite:", |
90
|
|
|
"description" => "Here you can place a description.", |
91
|
|
|
"size" => 6, |
92
|
|
|
"options" => [ |
93
|
|
|
"tomato" => "tomato", |
94
|
|
|
"potato" => "potato", |
95
|
|
|
"apple" => "apple", |
96
|
|
|
"pear" => "pear", |
97
|
|
|
"banana" => "banana", |
98
|
|
|
], |
99
|
|
|
"checked" => ["potato", "pear"], |
100
|
|
|
], |
101
|
|
|
|
102
|
|
|
"color" => [ |
103
|
|
|
"type" => "color", |
104
|
|
|
"description" => "Here you can place a description.", |
105
|
|
|
"placeholder" => "Here is a placeholder", |
106
|
|
|
], |
107
|
|
|
|
108
|
|
|
"date" => [ |
109
|
|
|
"type" => "date", |
110
|
|
|
"description" => "Here you can place a description.", |
111
|
|
|
"placeholder" => "Here is a placeholder", |
112
|
|
|
], |
113
|
|
|
|
114
|
|
|
"datetime" => [ |
115
|
|
|
"type" => "datetime", |
116
|
|
|
"description" => "Here you can place a description.", |
117
|
|
|
"placeholder" => "Here is a placeholder", |
118
|
|
|
], |
119
|
|
|
|
120
|
|
|
"datetime-local" => [ |
121
|
|
|
"type" => "datetime-local", |
122
|
|
|
"description" => "Here you can place a description.", |
123
|
|
|
"placeholder" => "Here is a placeholder", |
124
|
|
|
], |
125
|
|
|
|
126
|
|
|
"time" => [ |
127
|
|
|
"type" => "time", |
128
|
|
|
"description" => "Here you can place a description.", |
129
|
|
|
"placeholder" => "Here is a placeholder", |
130
|
|
|
], |
131
|
|
|
|
132
|
|
|
"week" => [ |
133
|
|
|
"type" => "week", |
134
|
|
|
"description" => "Here you can place a description.", |
135
|
|
|
"placeholder" => "Here is a placeholder", |
136
|
|
|
], |
137
|
|
|
|
138
|
|
|
"month" => [ |
139
|
|
|
"type" => "month", |
140
|
|
|
"description" => "Here you can place a description.", |
141
|
|
|
"placeholder" => "Here is a placeholder", |
142
|
|
|
], |
143
|
|
|
|
144
|
|
|
"number" => [ |
145
|
|
|
"type" => "number", |
146
|
|
|
"description" => "Here you can place a description.", |
147
|
|
|
"placeholder" => "Here is a placeholder", |
148
|
|
|
], |
149
|
|
|
|
150
|
|
|
"range" => [ |
151
|
|
|
"type" => "range", |
152
|
|
|
"description" => "Here you can place a description.", |
153
|
|
|
"placeholder" => "Here is a placeholder", |
154
|
|
|
"value" => 42, |
155
|
|
|
"min" => 0, |
156
|
|
|
"max" => 100, |
157
|
|
|
"step" => 2, |
158
|
|
|
], |
159
|
|
|
|
160
|
|
|
"search" => [ |
161
|
|
|
"type" => "search", |
162
|
|
|
"label" => "Search:", |
163
|
|
|
"description" => "Here you can place a description.", |
164
|
|
|
"placeholder" => "Here is a placeholder", |
165
|
|
|
], |
166
|
|
|
|
167
|
|
|
"tel" => [ |
168
|
|
|
"type" => "tel", |
169
|
|
|
"description" => "Here you can place a description.", |
170
|
|
|
"placeholder" => "Here is a placeholder", |
171
|
|
|
], |
172
|
|
|
|
173
|
|
|
"email" => [ |
174
|
|
|
"type" => "email", |
175
|
|
|
"description" => "Here you can place a description.", |
176
|
|
|
"placeholder" => "Here is a placeholder", |
177
|
|
|
], |
178
|
|
|
|
179
|
|
|
"url" => [ |
180
|
|
|
"type" => "url", |
181
|
|
|
"description" => "Here you can place a description.", |
182
|
|
|
"placeholder" => "Here is a placeholder", |
183
|
|
|
], |
184
|
|
|
|
185
|
|
|
"file-multiple" => [ |
186
|
|
|
"type" => "file-multiple", |
187
|
|
|
"description" => "Here you can place a description.", |
188
|
|
|
], |
189
|
|
|
|
190
|
|
|
"reset" => [ |
191
|
|
|
"type" => "reset", |
192
|
|
|
], |
193
|
|
|
|
194
|
|
|
"button" => [ |
195
|
|
|
"type" => "button", |
196
|
|
|
], |
197
|
|
|
|
198
|
|
|
"submit" => [ |
199
|
|
|
"type" => "submit", |
200
|
|
|
"value" => "Submit", |
201
|
|
|
"callback" => [$this, "callbackSubmit"] |
202
|
|
|
], |
203
|
|
|
] |
204
|
|
|
); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
|
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Callback for submit-button which should return true if it could |
211
|
|
|
* carry out its work and false if something failed. |
212
|
|
|
* |
213
|
|
|
* @return boolean true if okey, false if something went wrong. |
214
|
|
|
*/ |
215
|
|
|
public function callbackSubmit() |
216
|
|
|
{ |
217
|
|
|
// These return a single value |
218
|
|
|
// Type checkbox returns true if checked |
219
|
|
|
$elements = [ |
220
|
|
|
// HTML401. |
221
|
|
|
"text", "password", "hidden", "file", "textarea", "select", |
222
|
|
|
"radio", "checkbox", |
223
|
|
|
// HTML5 |
224
|
|
|
"color", "date", "datetime", "datetime-local", "time", |
225
|
|
|
"week", "month", "number", "range", "search", "tel", |
226
|
|
|
"email", "url", "file-multiple", |
227
|
|
|
]; |
228
|
|
|
foreach ($elements as $name) { |
229
|
|
|
$this->form->addOutput( |
230
|
|
|
"$name has value: " |
231
|
|
|
. $this->form->value($name) |
232
|
|
|
. "</br>" |
233
|
|
|
); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
// Select multiple returns an array |
237
|
|
|
$elements = [ |
238
|
|
|
"selectm", |
239
|
|
|
]; |
240
|
|
View Code Duplication |
foreach ($elements as $name) { |
|
|
|
|
241
|
|
|
$this->form->addOutput( |
242
|
|
|
"$name has value: " |
243
|
|
|
. implode($this->form->value($name), ", ") |
244
|
|
|
. "</br>" |
245
|
|
|
); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
// Remember values during resubmit, useful when failing (retunr false) |
249
|
|
|
// and asking the user to resubmit the form. |
250
|
|
|
$this->form->rememberValues(); |
251
|
|
|
|
252
|
|
|
return true; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
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.