Skip to content

Commit 48df021

Browse files
committed
RadioList, CheckboxList: getControlPart() and getLabelPart() throw exception for invalid key
1 parent 31d7646 commit 48df021

4 files changed

Lines changed: 58 additions & 8 deletions

File tree

src/Forms/Controls/CheckboxList.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Nette;
1111
use Nette\Utils\Html;
1212
use Stringable;
13-
use function array_flip, array_key_first, array_keys, array_merge, explode, func_num_args, in_array, is_array, key, substr;
13+
use function array_flip, array_key_exists, array_key_first, array_keys, array_merge, explode, func_num_args, in_array, is_array, is_string, key, substr;
1414

1515

1616
/**
@@ -85,6 +85,10 @@ public function getLabel($caption = null): Html
8585
public function getControlPart($key = null): Html
8686
{
8787
$key = key([(string) $key => null]);
88+
if (!array_key_exists($key, $this->getItems())) {
89+
throw new Nette\InvalidArgumentException("Item '$key' does not exist in field '{$this->getName()}'.");
90+
}
91+
8892
return parent::getControl()->addAttributes([
8993
'id' => $this->getHtmlId() . '-' . $key,
9094
'checked' => in_array($key, (array) $this->value, strict: true),
@@ -100,10 +104,17 @@ public function getControlPart($key = null): Html
100104
*/
101105
public function getLabelPart($key = null): Html
102106
{
107+
if (!func_num_args()) {
108+
return $this->getLabel();
109+
}
110+
111+
$key = key([(string) $key => null]);
112+
if (!array_key_exists($key, $this->getItems())) {
113+
throw new Nette\InvalidArgumentException("Item '$key' does not exist in field '{$this->getName()}'.");
114+
}
115+
103116
$itemLabel = clone $this->itemLabel;
104-
return func_num_args()
105-
? $itemLabel->setText($this->translate($this->getItems()[$key]))->for($this->getHtmlId() . '-' . $key)
106-
: $this->getLabel();
117+
return $itemLabel->setText($this->translate($this->getItems()[$key]))->for($this->getHtmlId() . '-' . $key);
107118
}
108119

109120

src/Forms/Controls/RadioList.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Nette;
1111
use Nette\Utils\Html;
1212
use Stringable;
13-
use function array_merge, func_num_args, in_array, is_array, key;
13+
use function array_key_exists, array_key_first, array_merge, func_num_args, in_array, is_array, key;
1414

1515

1616
/**
@@ -82,6 +82,10 @@ public function getLabel($caption = null): Html
8282
public function getControlPart($key = null): Html
8383
{
8484
$key = key([(string) $key => null]);
85+
if (!array_key_exists($key, $this->getItems())) {
86+
throw new Nette\InvalidArgumentException("Item '$key' does not exist in field '{$this->getName()}'.");
87+
}
88+
8589
return parent::getControl()->addAttributes([
8690
'id' => $this->getHtmlId() . '-' . $key,
8791
'checked' => in_array($key, (array) $this->value, strict: true),
@@ -96,10 +100,17 @@ public function getControlPart($key = null): Html
96100
*/
97101
public function getLabelPart($key = null): Html
98102
{
103+
if (!func_num_args()) {
104+
return $this->getLabel();
105+
}
106+
107+
$key = key([(string) $key => null]);
108+
if (!array_key_exists($key, $this->getItems())) {
109+
throw new Nette\InvalidArgumentException("Item '$key' does not exist in field '{$this->getName()}'.");
110+
}
111+
99112
$itemLabel = clone $this->itemLabel;
100-
return func_num_args()
101-
? $itemLabel->setText($this->translate($this->getItems()[$key]))->for($this->getHtmlId() . '-' . $key)
102-
: $this->getLabel();
113+
return $itemLabel->setText($this->translate($this->getItems()[$key]))->for($this->getHtmlId() . '-' . $key);
103114
}
104115

105116

tests/Controls/CheckboxList.render.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,17 @@ test('item label prototype styling', function () {
190190
Assert::same('<label class="foo" for="frm-list-a">b</label>', (string) $input->getLabelPart('a'));
191191
Assert::same('<label class="foo"><input type="checkbox" name="list[]" value="a">b</label>', (string) $input->getControl());
192192
});
193+
194+
195+
testException('invalid key in getControlPart', function () {
196+
$form = new Form;
197+
$input = $form->addCheckboxList('list', 'Label', ['a' => 'First']);
198+
$input->getControlPart('unknown');
199+
}, Nette\InvalidArgumentException::class, "Item 'unknown' does not exist in field 'list'.");
200+
201+
202+
testException('invalid key in getLabelPart', function () {
203+
$form = new Form;
204+
$input = $form->addCheckboxList('list', 'Label', ['a' => 'First']);
205+
$input->getLabelPart('unknown');
206+
}, Nette\InvalidArgumentException::class, "Item 'unknown' does not exist in field 'list'.");

tests/Controls/RadioList.render.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,17 @@ test('radio list control options', function () {
191191
$input->getControl();
192192
Assert::true($input->getOption('rendered'));
193193
});
194+
195+
196+
testException('invalid key in getControlPart', function () {
197+
$form = new Form;
198+
$input = $form->addRadioList('list', 'Label', ['a' => 'First']);
199+
$input->getControlPart('unknown');
200+
}, Nette\InvalidArgumentException::class, "Item 'unknown' does not exist in field 'list'.");
201+
202+
203+
testException('invalid key in getLabelPart', function () {
204+
$form = new Form;
205+
$input = $form->addRadioList('list', 'Label', ['a' => 'First']);
206+
$input->getLabelPart('unknown');
207+
}, Nette\InvalidArgumentException::class, "Item 'unknown' does not exist in field 'list'.");

0 commit comments

Comments
 (0)