-
-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathControls.CheckboxList.render.phpt
More file actions
194 lines (142 loc) · 6.48 KB
/
Copy pathControls.CheckboxList.render.phpt
File metadata and controls
194 lines (142 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* Test: Nette\Forms\Controls\CheckboxList.
*/
declare(strict_types=1);
use Nette\Forms\Form;
use Nette\Utils\Html;
use Tester\Assert;
require __DIR__ . '/../bootstrap.php';
class Translator implements Nette\Localization\ITranslator
{
public function translate($s, int $count = null): string
{
return strtoupper($s);
}
}
test(function () {
$form = new Form;
$input = $form->addCheckboxList('list', 'Label', [
'a' => 'First',
0 => 'Second',
]);
Assert::type(Html::class, $input->getLabel());
Assert::same('<label>Label</label>', (string) $input->getLabel());
Assert::same('<label>Another label</label>', (string) $input->getLabel('Another label'));
Assert::type(Html::class, $input->getLabelPart(0));
Assert::same('<label for="frm-list-0">Second</label>', (string) $input->getLabelPart(0));
Assert::type(Html::class, $input->getControl());
Assert::same('<label><input type="checkbox" name="list[]" value="a">First</label><br><label><input type="checkbox" name="list[]" value="0">Second</label>', (string) $input->getControl());
Assert::type(Html::class, $input->getControlPart(0));
Assert::same('<input type="checkbox" name="list[]" id="frm-list-0" value="0">', (string) $input->getControlPart(0));
});
test(function () { // checked
$form = new Form;
$input = $form->addCheckboxList('list', 'Label', [
'a' => 'First',
0 => 'Second',
])->setCurrentValue(0);
Assert::same('<label><input type="checkbox" name="list[]" value="a">First</label><br><label><input type="checkbox" name="list[]" checked value="0">Second</label>', (string) $input->getControl());
});
test(function () { // translator
$form = new Form;
$input = $form->addCheckboxList('list', 'Label', [
'a' => 'First',
0 => 'Second',
]);
$input->setTranslator(new Translator);
Assert::same('<label>Label</label>', (string) $input->getLabel());
Assert::same('<label>Another label</label>', (string) $input->getLabel('Another label'));
Assert::same('<label for="frm-list-0">SECOND</label>', (string) $input->getLabelPart(0));
Assert::same('<label><input type="checkbox" name="list[]" value="a">FIRST</label><br><label><input type="checkbox" name="list[]" value="0">SECOND</label>', (string) $input->getControl());
Assert::same('<input type="checkbox" name="list[]" id="frm-list-0" value="0">', (string) $input->getControlPart(0));
});
test(function () { // Html
$form = new Form;
$input = $form->addCheckboxList('list', Html::el('b', 'Label'), [
'a' => Html::el('b', 'First'),
]);
$input->setTranslator(new Translator);
Assert::same('<label><b>Label</b></label>', (string) $input->getLabel());
Assert::same('<label><b>Another label</b></label>', (string) $input->getLabel(Html::el('b', 'Another label')));
Assert::same('<label><input type="checkbox" name="list[]" value="a"><b>First</b></label>', (string) $input->getControl());
Assert::same('<input type="checkbox" name="list[]" id="frm-list-a" value="a">', (string) $input->getControlPart('a'));
});
test(function () { // validation rules
$form = new Form;
$input = $form->addCheckboxList('list', 'Label', [
'a' => 'First',
0 => 'Second',
])->setRequired('required');
Assert::same('<label><input type="checkbox" name="list[]" data-nette-rules=\'[{"op":":filled","msg":"required"}]\' value="a">First</label><br><label><input type="checkbox" name="list[]" value="0">Second</label>', (string) $input->getControl());
Assert::same('<input type="checkbox" name="list[]" id="frm-list-0" data-nette-rules=\'[{"op":":filled","msg":"required"}]\' value="0">', (string) $input->getControlPart(0));
});
test(function () { // container
$form = new Form;
$container = $form->addContainer('container');
$input = $container->addCheckboxList('list', 'Label', [
'a' => 'First',
0 => 'Second',
]);
Assert::same('<label><input type="checkbox" name="container[list][]" value="a">First</label><br><label><input type="checkbox" name="container[list][]" value="0">Second</label>', (string) $input->getControl());
});
test(function () { // separator prototype
$form = new Form;
$input = $form->addCheckboxList('list', null, [
'a' => 'b',
]);
$input->getSeparatorPrototype()->setName('div');
Assert::same('<div><label><input type="checkbox" name="list[]" value="a">b</label></div>', (string) $input->getControl());
});
test(function () { // disabled all
$form = new Form;
$input = $form->addCheckboxList('list', 'Label', [
'a' => 'First',
0 => 'Second',
])->setDisabled(true);
Assert::same('<label><input type="checkbox" name="list[]" disabled value="a">First</label><br><label><input type="checkbox" name="list[]" disabled value="0">Second</label>', (string) $input->getControl());
});
test(function () { // disabled one
$form = new Form;
$input = $form->addCheckboxList('list', 'Label', [
'a' => 'First',
0 => 'Second',
])->setDisabled(['a']);
Assert::same('<label><input type="checkbox" name="list[]" disabled value="a">First</label><br><label><input type="checkbox" name="list[]" value="0">Second</label>', (string) $input->getControl());
Assert::same('<input type="checkbox" name="list[]" id="frm-list-a" disabled value="a">', (string) $input->getControlPart('a'));
});
test(function () { // numeric key as string & getControlPart
$form = new Form;
$input = $form->addCheckboxList('list', 'Label', [
1 => 'First',
2 => 'Second',
])->setDefaultValue(1);
Assert::same('<input type="checkbox" name="list[]" id="frm-list-1" checked value="1">', (string) $input->getControlPart('1'));
});
test(function () { // container prototype
$form = new Form;
$input = $form->addCheckboxList('list', null, [
'a' => 'b',
]);
$input->getSeparatorPrototype()->setName('hr');
$input->getContainerPrototype()->setName('div');
Assert::same('<div><label><input type="checkbox" name="list[]" value="a">b</label></div>', (string) $input->getControl());
});
test(function () { // rendering options
$form = new Form;
$input = $form->addCheckboxList('list');
Assert::same('checkbox', $input->getOption('type'));
Assert::null($input->getOption('rendered'));
$input->getControl();
Assert::true($input->getOption('rendered'));
});
test(function () { // item label prototype
$form = new Form;
$input = $form->addCheckboxList('list', null, [
'a' => 'b',
]);
$input->getItemLabelPrototype()->class('foo');
Assert::same('<label></label>', (string) $input->getLabel());
Assert::same('<label class="foo" for="frm-list-a">b</label>', (string) $input->getLabelPart('a'));
Assert::same('<label class="foo"><input type="checkbox" name="list[]" value="a">b</label>', (string) $input->getControl());
});