Skip to content

Commit 3b543ff

Browse files
committed
RadioList: is rendered without attribute ID; added $generateId for back compatibility (BC BREAK!)
1 parent 2b5e6c3 commit 3b543ff

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

src/Forms/Controls/RadioList.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
*/
2323
class RadioList extends ChoiceControl
2424
{
25+
/** @var bool */
26+
public $generateId = FALSE;
27+
2528
/** @var Nette\Utils\Html separator element template */
2629
protected $separator;
2730

@@ -98,19 +101,22 @@ public function getControl($key = NULL)
98101
}
99102

100103
$input = parent::getControl();
104+
$items = $this->getItems();
101105
$ids = array();
102-
foreach ($this->getItems() as $value => $label) {
103-
$ids[$value] = $input->id . '-' . $value;
106+
if ($this->generateId) {
107+
foreach ($items as $value => $label) {
108+
$ids[$value] = $input->id . '-' . $value;
109+
}
104110
}
105111

106112
return $this->container->setHtml(
107113
Nette\Forms\Helpers::createInputList(
108-
$this->translate($this->getItems()),
114+
$this->translate($items),
109115
array_merge($input->attrs, array(
110116
'id:' => $ids,
111117
'checked?' => $this->value,
112118
'disabled:' => $this->disabled,
113-
'data-nette-rules:' => array(key($ids) => $input->attrs['data-nette-rules']),
119+
'data-nette-rules:' => array(key($items) => $input->attrs['data-nette-rules']),
114120
)),
115121
array('for:' => $ids) + $this->itemLabel->attrs,
116122
$this->separator

tests/Forms/Controls.RadioList.render.phpt

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

3838
Assert::type('Nette\Utils\Html', $input->getControl());
39-
Assert::same('<label for="frm-list-a"><input type="radio" name="list" id="frm-list-a" value="a">First</label><br><label for="frm-list-0"><input type="radio" name="list" id="frm-list-0" value="0">Second</label>', (string) $input->getControl());
39+
Assert::same('<label><input type="radio" name="list" value="a">First</label><br><label><input type="radio" name="list" value="0">Second</label>', (string) $input->getControl());
4040

4141
Assert::type('Nette\Utils\Html', $input->getControlPart(0));
4242
Assert::same('<input type="radio" 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 for="frm-list-a"><input type="radio" name="list" id="frm-list-a" value="a">First</label><br><label for="frm-list-0"><input type="radio" name="list" id="frm-list-0" checked value="0">Second</label>', (string) $input->getControl());
53+
Assert::same('<label><input type="radio" name="list" value="a">First</label><br><label><input type="radio" 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 for="frm-list-a"><input type="radio" name="list" id="frm-list-a" value="a">FIRST</label><br><label for="frm-list-0"><input type="radio" name="list" id="frm-list-0" value="0">SECOND</label>', (string) $input->getControl());
69+
Assert::same('<label><input type="radio" name="list" value="a">FIRST</label><br><label><input type="radio" name="list" value="0">SECOND</label>', (string) $input->getControl());
7070
Assert::same('<input type="radio" 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 for="frm-list-a"><input type="radio" name="list" id="frm-list-a" value="a"><b>First</b></label>', (string) $input->getControl());
84+
Assert::same('<label><input type="radio" name="list" value="a"><b>First</b></label>', (string) $input->getControl());
8585
Assert::same('<input type="radio" 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 for="frm-list-a"><input type="radio" name="list" required id="frm-list-a" data-nette-rules=\'[{"op":":filled","msg":"required"}]\' value="a">First</label><br><label for="frm-list-0"><input type="radio" name="list" required id="frm-list-0" value="0">Second</label>', (string) $input->getControl());
96+
Assert::same('<label><input type="radio" name="list" required data-nette-rules=\'[{"op":":filled","msg":"required"}]\' value="a">First</label><br><label><input type="radio" name="list" required value="0">Second</label>', (string) $input->getControl());
9797
Assert::same('<input type="radio" name="list" id="frm-list-0" required 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 for="frm-container-list-a"><input type="radio" name="container[list]" id="frm-container-list-a" value="a">First</label><br><label for="frm-container-list-0"><input type="radio" name="container[list]" id="frm-container-list-0" value="0">Second</label>', (string) $input->getControl());
109+
Assert::same('<label><input type="radio" name="container[list]" value="a">First</label><br><label><input type="radio" name="container[list]" value="0">Second</label>', (string) $input->getControl());
110110
});
111111

112112

@@ -118,7 +118,7 @@ test(function() { // container prototype
118118
$input->getSeparatorPrototype()->setName('hr');
119119
$input->getContainerPrototype()->setName('div');
120120

121-
Assert::same('<div><label for="frm-list-a"><input type="radio" name="list" id="frm-list-a" value="a">b</label></div>', (string) $input->getControl());
121+
Assert::same('<div><label><input type="radio" name="list" value="a">b</label></div>', (string) $input->getControl());
122122
});
123123

124124

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

132-
Assert::same('<label for="frm-list-a"><input type="radio" name="list" disabled id="frm-list-a" value="a">First</label><br><label for="frm-list-0"><input type="radio" name="list" disabled id="frm-list-0" value="0">Second</label>', (string) $input->getControl());
132+
Assert::same('<label><input type="radio" name="list" disabled value="a">First</label><br><label><input type="radio" name="list" disabled value="0">Second</label>', (string) $input->getControl());
133133
});
134134

135135

@@ -140,7 +140,7 @@ test(function() { // disabled one
140140
0 => 'Second',
141141
))->setDisabled(array('a'));
142142

143-
Assert::same('<label for="frm-list-a"><input type="radio" name="list" id="frm-list-a" disabled value="a">First</label><br><label for="frm-list-0"><input type="radio" name="list" id="frm-list-0" value="0">Second</label>', (string) $input->getControl());
143+
Assert::same('<label><input type="radio" name="list" disabled value="a">First</label><br><label><input type="radio" name="list" value="0">Second</label>', (string) $input->getControl());
144144
Assert::same('<input type="radio" name="list" id="frm-list-a" disabled value="a">', (string) $input->getControlPart('a'));
145145
});
146146

@@ -152,5 +152,17 @@ test(function() { // item label prototype
152152
));
153153
$input->getItemLabelPrototype()->class("foo");
154154

155-
Assert::same('<label class="foo" for="frm-list-a"><input type="radio" name="list" id="frm-list-a" value="a">b</label>', (string) $input->getControl());
155+
Assert::same('<label class="foo"><input type="radio" name="list" value="a">b</label>', (string) $input->getControl());
156+
});
157+
158+
159+
test(function() { // forced ID
160+
$form = new Form;
161+
$input = $form->addRadioList('list', 'Label', array(
162+
'a' => 'First',
163+
0 => 'Second',
164+
));
165+
$input->generateId = TRUE;
166+
167+
Assert::same('<label for="frm-list-a"><input type="radio" name="list" id="frm-list-a" value="a">First</label><br><label for="frm-list-0"><input type="radio" name="list" id="frm-list-0" value="0">Second</label>', (string) $input->getControl());
156168
});

tests/Forms/Forms.renderer.1.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<tr>
2626
<th><label>Your gender:</label></th>
2727

28-
<td><label for="frm-gender-m"><input type="radio" name="gender" id="frm-gender-m" value="m">male</label><br><label for="frm-gender-f"><input type="radio" name="gender" id="frm-gender-f" value="f">female</label></td>
28+
<td><label><input type="radio" name="gender" value="m">male</label><br><label><input type="radio" name="gender" value="f">female</label></td>
2929
</tr>
3030

3131
<tr>

0 commit comments

Comments
 (0)