-
-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathControls.SelectBox.render.phpt
More file actions
158 lines (117 loc) · 5.22 KB
/
Controls.SelectBox.render.phpt
File metadata and controls
158 lines (117 loc) · 5.22 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
<?php
/**
* Test: Nette\Forms\Controls\SelectBox.
*/
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->addSelect('list', 'Label', [
'a' => 'First',
0 => 'Second',
]);
Assert::type(Html::class, $input->getLabel());
Assert::same('<label for="frm-list">Label</label>', (string) $input->getLabel());
Assert::same('<label for="frm-list">Another label</label>', (string) $input->getLabel('Another label'));
Assert::type(Html::class, $input->getControl());
Assert::same('<select name="list" id="frm-list"><option value="a" selected>First</option><option value="0">Second</option></select>', (string) $input->getControl());
});
test(function () { // selected
$form = new Form;
$input = $form->addSelect('list', 'Label', [
'a' => 'First',
0 => 'Second',
])->setValue(0);
Assert::same('<select name="list" id="frm-list"><option value="a">First</option><option value="0" selected>Second</option></select>', (string) $input->getControl());
});
test(function () { // selected 2x
$form = new Form;
$input = $form->addSelect('list', 'Label', [
['a' => 'First'],
['a' => 'First'],
])->setValue('a');
Assert::same('<select name="list" id="frm-list"><optgroup label="0"><option value="a" selected>First</option></optgroup><optgroup label="1"><option value="a">First</option></optgroup></select>', (string) $input->getControl());
});
test(function () { // translator & groups
$form = new Form;
$input = $form->addSelect('list', 'Label', [
'a' => 'First',
'group' => ['Second', 'Third'],
])->setPrompt('Prompt');
$input->setTranslator(new Translator);
Assert::same('<label for="frm-list">Label</label>', (string) $input->getLabel());
Assert::same('<label for="frm-list">Another label</label>', (string) $input->getLabel('Another label'));
Assert::same('<select name="list" id="frm-list"><option value="">PROMPT</option><option value="a">FIRST</option><optgroup label="GROUP"><option value="0">SECOND</option><option value="1">THIRD</option></optgroup></select>', (string) $input->getControl());
});
test(function () { // Html with translator & groups
$form = new Form;
$input = $form->addSelect('list', Html::el('b', 'Label'), [
'a' => Html::el('option', 'First')->class('class'),
'group' => [Html::el('option', 'Second')],
])->setPrompt(Html::el('option', 'Prompt')->class('class'));
$input->setTranslator(new Translator);
Assert::same('<label for="frm-list"><b>Label</b></label>', (string) $input->getLabel());
Assert::same('<label for="frm-list"><b>Another label</b></label>', (string) $input->getLabel(Html::el('b', 'Another label')));
Assert::same('<select name="list" id="frm-list"><option class="class" value="">Prompt</option><option class="class" value="a">First</option><optgroup label="GROUP"><option value="0">Second</option></optgroup></select>', (string) $input->getControl());
});
test(function () { // validation rules
$form = new Form;
$input = $form->addSelect('list', 'Label', [
'a' => 'First',
0 => 'Second',
])->setRequired('required');
Assert::same('<select name="list" id="frm-list" required data-nette-rules=\'[{"op":":filled","msg":"required"}]\'><option value="a" selected>First</option><option value="0">Second</option></select>', (string) $input->getControl());
});
test(function () { // container
$form = new Form;
$container = $form->addContainer('container');
$input = $container->addSelect('list', 'Label', [
'a' => 'First',
0 => 'Second',
]);
Assert::same('<select name="container[list]" id="frm-container-list"><option value="a" selected>First</option><option value="0">Second</option></select>', (string) $input->getControl());
});
test(function () { // disabled all
$form = new Form;
$input = $form->addSelect('list', 'Label', [
'a' => 'First',
0 => 'Second',
])->setDisabled(true);
Assert::same('<select name="list" id="frm-list" disabled><option value="a">First</option><option value="0">Second</option></select>', (string) $input->getControl());
});
test(function () { // disabled one
$form = new Form;
$input = $form->addSelect('list', 'Label', [
'a' => 'First',
0 => 'Second',
])->setDisabled(['a']);
Assert::same('<select name="list" id="frm-list"><option value="a" disabled>First</option><option value="0">Second</option></select>', (string) $input->getControl());
});
test(function () { // rendering options
$form = new Form;
$input = $form->addSelect('list');
Assert::same('select', $input->getOption('type'));
Assert::null($input->getOption('rendered'));
$input->getControl();
Assert::true($input->getOption('rendered'));
});
test(function () {
$form = new Form;
$input = $form->addSelect('list', 'Label', [
1 => 'First',
2 => 'Second',
])->setValue(1);
$input->addOptionAttributes(['bar' => 'b', 'selected?' => 2, 'foo:' => [1 => 'a', 2 => 'b']]);
$input->addOptionAttributes(['bar' => 'c']);
Assert::same('<select name="list" id="frm-list"><option bar="c" value="1" selected foo="a">First</option><option bar="c" value="2" foo="b">Second</option></select>', (string) $input->getControl());
});