Skip to content

Commit 796858f

Browse files
enumagdg
authored andcommitted
CheckboxList: added containerPrototype and itemLabelPrototype [Closes #92]
1 parent 4de4487 commit 796858f

File tree

2 files changed

+93
-33
lines changed

2 files changed

+93
-33
lines changed

src/Forms/Controls/CheckboxList.php

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,58 @@
1515
* Set of checkboxes.
1616
*
1717
* @property-read Html $separatorPrototype
18+
* @property-read Html $containerPrototype
19+
* @property-read Html $itemLabelPrototype
1820
*/
1921
class CheckboxList extends MultiChoiceControl
2022
{
2123
/** @var Html separator element template */
2224
protected $separator;
2325

26+
/** @var Html container element template */
27+
protected $container;
2428

29+
/** @var Html item label template */
30+
protected $itemLabel;
31+
32+
33+
/**
34+
* @param string label
35+
* @param array options from which to choose
36+
*/
2537
public function __construct($label = NULL, array $items = NULL)
2638
{
2739
parent::__construct($label, $items);
2840
$this->control->type = 'checkbox';
41+
$this->container = Html::el();
2942
$this->separator = Html::el('br');
43+
$this->itemLabel = Html::el();
3044
}
3145

3246

3347
/**
3448
* Generates control's HTML element.
35-
* @return string
49+
* @return Html
3650
*/
3751
public function getControl()
3852
{
53+
$input = parent::getControl();
3954
$items = $this->getItems();
4055
reset($items);
41-
$input = parent::getControl();
42-
return Nette\Forms\Helpers::createInputList(
43-
$this->translate($items),
44-
array_merge($input->attrs, [
45-
'id' => NULL,
46-
'checked?' => $this->value,
47-
'disabled:' => $this->disabled,
48-
'required' => NULL,
49-
'data-nette-rules:' => [key($items) => $input->attrs['data-nette-rules']],
50-
]),
51-
$this->label->attrs,
52-
$this->separator
56+
57+
return $this->container->setHtml(
58+
Nette\Forms\Helpers::createInputList(
59+
$this->translate($items),
60+
array_merge($input->attrs, [
61+
'id' => NULL,
62+
'checked?' => $this->value,
63+
'disabled:' => $this->disabled,
64+
'required' => NULL,
65+
'data-nette-rules:' => [key($items) => $input->attrs['data-nette-rules']],
66+
]),
67+
$this->itemLabel->attrs,
68+
$this->separator
69+
)
5370
);
5471
}
5572

@@ -65,16 +82,6 @@ public function getLabel($caption = NULL)
6582
}
6683

6784

68-
/**
69-
* Returns separator HTML element template.
70-
* @return Html
71-
*/
72-
public function getSeparatorPrototype()
73-
{
74-
return $this->separator;
75-
}
76-
77-
7885
/**
7986
* @return Html
8087
*/
@@ -101,4 +108,34 @@ public function getLabelPart($key = NULL)
101108
: $this->getLabel();
102109
}
103110

111+
112+
/**
113+
* Returns separator HTML element template.
114+
* @return Html
115+
*/
116+
public function getSeparatorPrototype()
117+
{
118+
return $this->separator;
119+
}
120+
121+
122+
/**
123+
* Returns container HTML element template.
124+
* @return Html
125+
*/
126+
public function getContainerPrototype()
127+
{
128+
return $this->container;
129+
}
130+
131+
132+
/**
133+
* Returns item label HTML element template.
134+
* @return Html
135+
*/
136+
public function getItemLabelPrototype()
137+
{
138+
return $this->itemLabel;
139+
}
140+
104141
}

tests/Forms/Controls.CheckboxList.render.phpt

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ test(function () {
3535
Assert::type(Html::class, $input->getLabelPart(0));
3636
Assert::same('<label for="frm-list-0">Second</label>', (string) $input->getLabelPart(0));
3737

38-
Assert::type('string', $input->getControl());
39-
Assert::same('<label><input type="checkbox" name="list[]" value="a">First</label><br><label><input type="checkbox" name="list[]" value="0">Second</label>', $input->getControl());
38+
Assert::type(Html::class, $input->getControl());
39+
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());
4040

4141
Assert::type(Html::class, $input->getControlPart(0));
4242
Assert::same('<input type="checkbox" name="list[]" id="frm-list-0" value="0">', (string) $input->getControlPart(0));
@@ -50,7 +50,7 @@ test(function () { // checked
5050
0 => 'Second',
5151
])->setValue(0);
5252

53-
Assert::same('<label><input type="checkbox" name="list[]" value="a">First</label><br><label><input type="checkbox" name="list[]" checked value="0">Second</label>', $input->getControl());
53+
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());
5454
});
5555

5656

@@ -66,7 +66,7 @@ test(function () { // translator
6666
Assert::same('<label>ANOTHER LABEL</label>', (string) $input->getLabel('Another label'));
6767
Assert::same('<label for="frm-list-0">SECOND</label>', (string) $input->getLabelPart(0));
6868

69-
Assert::same('<label><input type="checkbox" name="list[]" value="a">FIRST</label><br><label><input type="checkbox" name="list[]" value="0">SECOND</label>', $input->getControl());
69+
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());
7070
Assert::same('<input type="checkbox" name="list[]" id="frm-list-0" value="0">', (string) $input->getControlPart(0));
7171
});
7272

@@ -81,7 +81,7 @@ test(function () { // Html
8181
Assert::same('<label><b>Label</b></label>', (string) $input->getLabel());
8282
Assert::same('<label><b>Another label</b></label>', (string) $input->getLabel(Html::el('b', 'Another label')));
8383

84-
Assert::same('<label><input type="checkbox" name="list[]" value="a"><b>First</b></label>', $input->getControl());
84+
Assert::same('<label><input type="checkbox" name="list[]" value="a"><b>First</b></label>', (string) $input->getControl());
8585
Assert::same('<input type="checkbox" name="list[]" id="frm-list-a" value="a">', (string) $input->getControlPart('a'));
8686
});
8787

@@ -93,7 +93,7 @@ test(function () { // validation rules
9393
0 => 'Second',
9494
])->setRequired('required');
9595

96-
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>', $input->getControl());
96+
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());
9797
Assert::same('<input type="checkbox" name="list[]" id="frm-list-0" data-nette-rules=\'[{"op":":filled","msg":"required"}]\' value="0">', (string) $input->getControlPart(0));
9898
});
9999

@@ -106,7 +106,7 @@ test(function () { // container
106106
0 => 'Second',
107107
]);
108108

109-
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>', $input->getControl());
109+
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());
110110
});
111111

112112

@@ -117,7 +117,7 @@ test(function () { // separator prototype
117117
]);
118118
$input->getSeparatorPrototype()->setName('div');
119119

120-
Assert::same('<div><label><input type="checkbox" name="list[]" value="a">b</label></div>', $input->getControl());
120+
Assert::same('<div><label><input type="checkbox" name="list[]" value="a">b</label></div>', (string) $input->getControl());
121121
});
122122

123123

@@ -128,7 +128,7 @@ test(function () { // disabled all
128128
0 => 'Second',
129129
])->setDisabled(TRUE);
130130

131-
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>', $input->getControl());
131+
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());
132132
});
133133

134134

@@ -139,7 +139,7 @@ test(function () { // disabled one
139139
0 => 'Second',
140140
])->setDisabled(['a']);
141141

142-
Assert::same('<label><input type="checkbox" name="list[]" disabled value="a">First</label><br><label><input type="checkbox" name="list[]" value="0">Second</label>', $input->getControl());
142+
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());
143143
Assert::same('<input type="checkbox" name="list[]" id="frm-list-a" disabled value="a">', (string) $input->getControlPart('a'));
144144
});
145145

@@ -153,3 +153,26 @@ test(function () { // numeric key as string & getControlPart
153153

154154
Assert::same('<input type="checkbox" name="list[]" id="frm-list-1" checked value="1">', (string) $input->getControlPart('1'));
155155
});
156+
157+
158+
test(function () { // container prototype
159+
$form = new Form;
160+
$input = $form->addCheckboxList('list', NULL, [
161+
'a' => 'b',
162+
]);
163+
$input->getSeparatorPrototype()->setName('hr');
164+
$input->getContainerPrototype()->setName('div');
165+
166+
Assert::same('<div><label><input type="checkbox" name="list[]" value="a">b</label></div>', (string) $input->getControl());
167+
});
168+
169+
170+
test(function () { // item label prototype
171+
$form = new Form;
172+
$input = $form->addCheckboxList('list', NULL, [
173+
'a' => 'b',
174+
]);
175+
$input->getItemLabelPrototype()->class('foo');
176+
177+
Assert::same('<label class="foo"><input type="checkbox" name="list[]" value="a">b</label>', (string) $input->getControl());
178+
});

0 commit comments

Comments
 (0)