Skip to content

Commit 6fba2ab

Browse files
committed
RadioList & CheckboxList: getControlPart() normalizes key type [Closes #76][Closes #78]
1 parent 8d9fe09 commit 6fba2ab

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

src/Forms/Controls/CheckboxList.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public function getSeparatorPrototype()
8282
*/
8383
public function getControlPart($key)
8484
{
85+
$key = key(array((string) $key => NULL));
8586
return parent::getControl()->addAttributes(array(
8687
'id' => $this->getHtmlId() . '-' . $key,
8788
'checked' => in_array($key, (array) $this->value, TRUE),

src/Forms/Controls/RadioList.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ public function getLabel($caption = NULL)
136136
*/
137137
public function getControlPart($key)
138138
{
139+
$key = key(array((string) $key => NULL));
139140
return parent::getControl()->addAttributes(array(
140141
'id' => $this->getHtmlId() . '-' . $key,
141142
'checked' => in_array($key, (array) $this->value, TRUE),

tests/Forms/Controls.CheckboxList.render.phpt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,14 @@ test(function() { // disabled one
142142
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());
143143
Assert::same('<input type="checkbox" name="list[]" id="frm-list-a" disabled value="a">', (string) $input->getControlPart('a'));
144144
});
145+
146+
147+
test(function() { // numeric key as string & getControlPart
148+
$form = new Form;
149+
$input = $form->addCheckboxList('list', 'Label', array(
150+
1 => 'First',
151+
2 => 'Second',
152+
))->setDefaultValue(1);
153+
154+
Assert::same('<input type="checkbox" name="list[]" id="frm-list-1" checked value="1">', (string) $input->getControlPart('1'));
155+
});

tests/Forms/Controls.RadioList.render.phpt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,14 @@ test(function() { // forced ID
166166

167167
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());
168168
});
169+
170+
171+
test(function() { // numeric key as string & getControlPart
172+
$form = new Form;
173+
$input = $form->addRadioList('list', 'Label', array(
174+
1 => 'First',
175+
2 => 'Second',
176+
))->setDefaultValue(1);
177+
178+
Assert::same('<input type="radio" name="list" id="frm-list-1" checked value="1">', (string) $input->getControlPart('1'));
179+
});

0 commit comments

Comments
 (0)