Skip to content

Commit 9efa19f

Browse files
committed
RadioList, CheckboxList: getControlPart() and getLabelPart() throw exception for invalid key
- ChoiceControl, MultiChoiceControl: extracted getItem() method
1 parent 0cd1472 commit 9efa19f

6 files changed

Lines changed: 59 additions & 7 deletions

File tree

src/Forms/Controls/CheckboxList.php

Lines changed: 3 additions & 3 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, is_string, key, substr;
13+
use function array_flip, array_key_first, array_keys, array_merge, explode, func_num_args, in_array, is_array, is_string, substr;
1414

1515

1616
/**
@@ -86,7 +86,7 @@ public function getLabel($caption = null): Html
8686
*/
8787
public function getControlPart($key = null): Html
8888
{
89-
$key = key([(string) $key => null]);
89+
$this->getItem($key);
9090
return parent::getControl()->addAttributes([
9191
'id' => $this->getHtmlId() . '-' . $key,
9292
'checked' => in_array($key, (array) $this->value, strict: true),
@@ -104,7 +104,7 @@ public function getLabelPart($key = null): Html
104104
{
105105
$itemLabel = clone $this->itemLabel;
106106
return func_num_args()
107-
? $itemLabel->setText($this->translate($this->getItems()[$key]))->for($this->getHtmlId() . '-' . $key)
107+
? $itemLabel->setText($this->translate($this->getItem($key)))->for($this->getHtmlId() . '-' . $key)
108108
: $this->getLabel();
109109
}
110110

src/Forms/Controls/ChoiceControl.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,16 @@ public function checkDefaultValue(bool $value = true): static
153153
$this->checkDefaultValue = $value;
154154
return $this;
155155
}
156+
157+
158+
/**
159+
* Returns the item label for the given key, or throws an exception if the key does not exist.
160+
*/
161+
protected function getItem(mixed &$key): mixed
162+
{
163+
$key = key([(string) $key => null]);
164+
return array_key_exists($key, $this->items)
165+
? $this->items[$key]
166+
: throw new Nette\InvalidArgumentException("Item '$key' does not exist in field '{$this->getName()}'.");
167+
}
156168
}

src/Forms/Controls/MultiChoiceControl.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Nette\Forms\Controls;
99

1010
use Nette;
11-
use function array_combine, array_diff, array_fill_keys, array_flip, array_keys, array_map, count, get_debug_type, implode, is_array, is_scalar, sprintf, var_export;
11+
use function array_combine, array_diff, array_fill_keys, array_flip, array_key_exists, array_keys, array_map, count, get_debug_type, implode, is_array, is_scalar, key, sprintf, var_export;
1212

1313

1414
/**
@@ -168,4 +168,16 @@ public function checkDefaultValue(bool $value = true): static
168168
$this->checkDefaultValue = $value;
169169
return $this;
170170
}
171+
172+
173+
/**
174+
* Returns the item label for the given key, or throws an exception if the key does not exist.
175+
*/
176+
protected function getItem(mixed &$key): mixed
177+
{
178+
$key = key([(string) $key => null]);
179+
return array_key_exists($key, $this->items)
180+
? $this->items[$key]
181+
: throw new Nette\InvalidArgumentException("Item '$key' does not exist in field '{$this->getName()}'.");
182+
}
171183
}

src/Forms/Controls/RadioList.php

Lines changed: 3 additions & 3 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_first, array_merge, func_num_args, in_array, is_array;
1414

1515

1616
/**
@@ -81,7 +81,7 @@ public function getLabel($caption = null): Html
8181
*/
8282
public function getControlPart($key = null): Html
8383
{
84-
$key = key([(string) $key => null]);
84+
$this->getItem($key);
8585
return parent::getControl()->addAttributes([
8686
'id' => $this->getHtmlId() . '-' . $key,
8787
'checked' => in_array($key, (array) $this->value, strict: true),
@@ -98,7 +98,7 @@ public function getLabelPart($key = null): Html
9898
{
9999
$itemLabel = clone $this->itemLabel;
100100
return func_num_args()
101-
? $itemLabel->setText($this->translate($this->getItems()[$key]))->for($this->getHtmlId() . '-' . $key)
101+
? $itemLabel->setText($this->translate($this->getItem($key)))->for($this->getHtmlId() . '-' . $key)
102102
: $this->getLabel();
103103
}
104104

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)