CreateUserFormExample   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 243
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 148
c 1
b 0
f 0
dl 0
loc 243
rs 10
wmc 4

2 Methods

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