Completed
Push — master ( 5978c7...5a86a1 )
by Gabor
12:24
created

TestForm::getMainFieldset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 50
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 50
ccs 0
cts 34
cp 0
rs 9.3333
c 0
b 0
f 0
cc 1
eloc 26
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 5.6
6
 *
7
 * @copyright 2012 - 2016 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
namespace WebHemi\Form;
13
14
/**
15
 * Class TestForm
16
 */
17
class TestForm extends AbstractForm
18
{
19
    /**
20
     * Initialize form.
21
     *
22
     * @return void
23
     */
24
    protected function initForm()
25
    {
26
        // The best way to avoid autocomplete fields is to give unique name to the fields on every page load.
27
        $this->setUniqueFormNamePostfix(md5(time()));
28
        // Change some default attributes.
29
        $this->setAutocomplete(false);
30
31
32
        // Hidden field.
33
        $hiddenCsrf = new FormElement(FormElement::TAG_INPUT_HIDDEN, 'csrf');
34
        $hiddenCsrf->setValue('Some CSRF test value');
35
36
        // Fieldsets
37
        $fieldset1 = $this->getMainFieldset();
38
        $fieldset2 = $this->getLocationFieldset();
39
40
        // Submit button.
41
        $submit = new FormElement(FormElement::TAG_BUTTON_SUBMIT, 'submit', 'Login');
42
43
        // Assign elements and the fieldset to the form
44
        $this->addChildNode($hiddenCsrf)
45
            ->addChildNode($fieldset1)
46
            ->addChildNode($fieldset2)
47
            ->addChildNode($submit);
48
    }
49
50
    /**
51
     * Gets main fieldset
52
     *
53
     * @return FormElement
54
     */
55
    private function getMainFieldset()
56
    {
57
        // Fieldset
58
        $fieldset = new FormElement(FormElement::TAG_FIELDSET, 'info');
59
        // Legend for the fieldset.
60
        $legend = new FormElement(FormElement::TAG_LEGEND, 'info', 'Login form');
61
62
        // Text input with custom attribute
63
        $loginname = new FormElement(FormElement::TAG_INPUT_TEXT, 'username', 'Username');
64
        $loginname->setAttribute('placeholder', 'Your login name');
65
66
        // Password input
67
        $password = new FormElement(FormElement::TAG_INPUT_PASSWORD, 'password', 'Password');
68
69
        // Single-option checkbox.
70
        // For single-option radio boxes the ->setValue(1) is equals to this: ->setAttribute('checked', true);
71
        $checkboxRememberMe = new FormElement(FormElement::TAG_INPUT_CHECKBOX, 'remember_me', 'Remember Me');
72
        $checkboxRememberMe->setValue(1);
73
74
        // Radio group.
75
        $radioLanguage = new FormElement(FormElement::TAG_INPUT_RADIO, 'language', 'Select language');
76
        $radioLanguage->setValue('es')
77
            ->setOptions(
78
                [
79
                    ['label' => 'English', 'value' => 'en'],
80
                    ['label' => 'German', 'value' => 'de'],
81
                    ['label' => 'Spanish', 'value' => 'es'],
82
                ]
83
            );
84
85
        // Checkbox group.
86
        $checkboxTerms = new FormElement(FormElement::TAG_INPUT_CHECKBOX, 'terms', 'Terms and conditions');
87
        $checkboxTerms->setOptions(
88
            [
89
                ['label' => 'I am human.', 'value' => 'human'],
90
                ['label' => 'I have read the T&C.', 'value' => 'terms_read', 'checked' => false],
91
                ['label' => 'I want newsletters.', 'value' => 'terms_newsletter', 'checked' => true],
92
            ]
93
        );
94
95
        // Assign elements to the fieldset
96
        $fieldset->addChildNode($legend)
97
            ->addChildNode($loginname)
98
            ->addChildNode($password)
99
            ->addChildNode($checkboxRememberMe)
100
            ->addChildNode($radioLanguage)
101
            ->addChildNode($checkboxTerms);
102
103
        return $fieldset;
104
    }
105
106
    /**
107
     * Gets location fieldset.
108
     *
109
     * @return FormElement
110
     */
111
    private function getLocationFieldset()
112
    {
113
        // Fieldset
114
        $fieldset = new FormElement(FormElement::TAG_FIELDSET, 'location');
115
        // Legend for the fieldset.
116
        $legend = new FormElement(FormElement::TAG_LEGEND, 'location', 'Location');
117
118
        // Select box with no multi selection and with no option groups
119
        $select1 = new FormElement(FormElement::TAG_SELECT, 'country', 'I live in:');
120
        $select1->setOptions(
121
            [
122
                ['label' => 'Hungary', 'value' => 'hu'],
123
                ['label' => 'Germany', 'value' => 'de', 'checked' => true],
124
                ['label' => 'Austria', 'value' => 'at'],
125
            ]
126
        );
127
128
        // Select box with no multi selection and WITH option groups
129
        $select2 = new FormElement(FormElement::TAG_SELECT, 'dream', 'I\'d like live in:');
130
        $select2->setOptions(
131
            [
132
                ['label' => 'Anywhere', 'value' => '*'],
133
                ['label' => 'Hungary', 'value' => 'hu', 'group' => 'Europe'],
134
                ['label' => 'USA', 'value' => 'us', 'group' => 'America'],
135
                ['label' => 'Germany', 'value' => 'de', 'group' => 'Europe'],
136
                ['label' => 'Austria', 'value' => 'at', 'group' => 'Europe'],
137
                ['label' => 'Canada', 'value' => 'ca', 'group' => 'America'],
138
                ['label' => 'Switzerland', 'value' => 'ch', 'group' => 'Europe', 'checked' => true],
139
                ['label' => 'France', 'value' => 'fr', 'group' => 'Europe'],
140
                ['label' => 'Spain', 'value' => 'es', 'group' => 'Europe'],
141
            ]
142
        );
143
144
        // Select box WITH multi selection.
145
        $select3 = new FormElement(FormElement::TAG_SELECT, 'past', 'I\'ve already lived in:');
146
        $select3->setOptions(
147
            [
148
                ['label' => 'Hungary', 'value' => 'hu', 'group' => 'Europe', 'checked' => true],
149
                ['label' => 'USA', 'value' => 'us', 'group' => 'America'],
150
                ['label' => 'Germany', 'value' => 'de', 'group' => 'Europe', 'checked' => true],
151
                ['label' => 'Austria', 'value' => 'at', 'group' => 'Europe'],
152
                ['label' => 'Canada', 'value' => 'ca', 'group' => 'America'],
153
                ['label' => 'Switzerland', 'value' => 'ch', 'group' => 'Europe'],
154
                ['label' => 'France', 'value' => 'fr', 'group' => 'Europe'],
155
                ['label' => 'Spain', 'value' => 'es', 'group' => 'Europe'],
156
            ]
157
        )
158
            ->setAttribute('multiple', true)
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
159
            ->setAttribute('size', 10);
160
161
        // Assign elements to the fieldset
162
        $fieldset->addChildNode($legend)
163
            ->addChildNode($select1)
164
            ->addChildNode($select2)
165
            ->addChildNode($select3);
166
167
        return $fieldset;
168
    }
169
}
170