LearnZF2Form::init()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 115
Code Lines 76

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 3 Features 0
Metric Value
c 3
b 3
f 0
dl 0
loc 115
rs 8.2857
cc 1
eloc 76
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the MIT license.
18
 */
19
namespace LearnZF2FormUsage\Form;
20
21
use Zend\Form\Form;
22
use Zend\InputFilter\InputFilterProviderInterface;
23
24
class LearnZF2Form extends Form implements InputFilterProviderInterface
25
{
26
    public function __construct()
27
    {
28
        parent::__construct('formname');
29
    }
30
31
    public function init()
32
    {
33
        $this->setAttribute('method', 'post');
34
35
        $this->add([
36
            'name' => 'id',
37
            'attributes' => [
38
                'type' => 'hidden',
39
            ],
40
        ]);
41
42
        $this->add([
43
            'name' => 'name',
44
            'attributes' => [
45
                'type' => 'text',
46
                'class' => 'form-control',
47
            ],
48
            'options' => [
49
                'label' => 'Name',
50
            ],
51
        ]);
52
53
        $this->add([
54
            'type' => 'Zend\Form\Element\Select',
55
            'name' => 'gender',
56
            'options' => [
57
                'label' => 'Gender',
58
                'value_options' => [
59
                    '1' => 'Select your gender',
60
                    '2' => 'Female',
61
                    '3' => 'Male',
62
                ],
63
            ],
64
            'attributes' => [
65
                'class' => 'form-control',
66
                'value' => '1', //set selected to '1'
67
            ],
68
        ]);
69
70
        $this->add([
71
            'type' => 'Zend\Form\Element\MultiCheckbox',
72
            'name' => 'hobby',
73
            'options' => [
74
                'label' => 'Please choose one/more of the hobbies',
75
                'value_options' => [
76
                    '1' => 'Cooking',
77
                    '2' => 'Writing',
78
                    '3' => 'Others',
79
                ],
80
            ],
81
            'attributes' => [
82
                'class' => 'form-control',
83
                'value' => '1', //set checked to '1'
84
            ],
85
        ]);
86
87
        $this->add([
88
            'type' => 'Zend\Form\Element\Email',
89
            'name' => 'email',
90
            'options' => [
91
                'label' => 'Email',
92
            ],
93
            'attributes' => [
94
                'placeholder' => '[email protected]',
95
            ],
96
        ]);
97
98
        $this->add([
99
            'type' => 'Zend\Form\Element\Date',
100
            'name' => 'birth',
101
            'options' => [
102
                'label' => 'Birth ( Y/m/d )',
103
            ],
104
            'attributes' => [
105
                'class' => 'form-control',
106
            ],
107
        ]);
108
109
        $this->add([
110
            'name' => 'address',
111
            'attributes' => [
112
                'class' => 'form-control',
113
                'type' => 'textarea',
114
            ],
115
            'options' => [
116
                'label' => 'Address',
117
            ],
118
        ]);
119
120
        $this->add([
121
            'type' => 'Zend\Form\Element\Radio',
122
            'name' => 'direction',
123
            'options' => [
124
                'label' => 'Please choose one of the directions',
125
                'value_options' => [
126
                    '1' => 'Programming',
127
                    '2' => 'Design',
128
                ],
129
            ],
130
            'attributes' => [
131
                'class' => 'form-control',
132
                'value' => '1', //set checked to '1'
133
            ],
134
        ]);
135
136
        $this->add([
137
            'name' => 'submit',
138
            'attributes' => [
139
                'class' => 'form-control',
140
                'type' => 'submit',
141
                'value' => 'Go',
142
                'id' => 'submitbutton',
143
            ],
144
        ]);
145
    }
146
147
    public function getInputFilterSpecification()
148
    {
149
        return [
150
            [
151
                'name' => 'id',
152
                'required' => false,
153
                'allow_empty' => true,
154
                'filters' => [
155
                    ['name' => 'Int'],
156
                ],
157
            ],
158
            [
159
                'name' => 'name',
160
                'required' => true,
161
                'filters' => [
162
                    ['name' => 'StripTags'],
163
                    ['name' => 'StringTrim'],
164
                ],
165
                'validators' => [
166
                    [
167
                        'name' => 'StringLength',
168
                        'options' => [
169
                            'encoding' => 'UTF-8',
170
                            'min' => 5,
171
                            'max' => 255,
172
                        ],
173
                    ],
174
                ],
175
            ],
176
            [
177
                'name' => 'gender',
178
                'validators' => [
179
                    [
180
                        'name' => 'InArray',
181
                        'options' => [
182
                            'haystack' => [2, 3],
183
                            'messages' => [
184
                                'notInArray' => 'Please select your gender !',
185
                            ],
186
                        ],
187
                    ],
188
                ],
189
            ],
190
            [
191
                'name' => 'hobby',
192
                'required' => true,
193
            ],
194
            [
195
                'name' => 'email',
196
                'validators' => [
197
                    [
198
                        'name' => 'EmailAddress',
199
                    ],
200
                ],
201
            ],
202
            [
203
                'name' => 'birth',
204
                'validators' => [
205
                    [
206
                        'name' => 'Between',
207
                        'options' => [
208
                            'min' => '1970-01-01',
209
                            'max' => date('Y-m-d'),
210
                        ],
211
                    ],
212
                ],
213
            ],
214
            [
215
                'name' => 'address',
216
                'required' => true,
217
                'filters' => [
218
                    ['name' => 'StripTags'],
219
                    ['name' => 'StringTrim'],
220
                ],
221
                'validators' => [
222
                    [
223
                        'name' => 'StringLength',
224
                        'options' => [
225
                            'encoding' => 'UTF-8',
226
                            'min' => 5,
227
                            'max' => 255,
228
                        ],
229
                    ],
230
                ],
231
            ],
232
            [
233
                'name' => 'direction',
234
                'required' => true,
235
            ],
236
        ];
237
    }
238
}
239