Skip to content

Commit fa966a6

Browse files
committed
ChoiceControl, MultiChoiceControl: added $checkAllowedValues [Closes #28]
1 parent c2b37cd commit fa966a6

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

src/Forms/Controls/ChoiceControl.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
*/
2020
abstract class ChoiceControl extends BaseControl
2121
{
22+
/** @var bool */
23+
public $checkAllowedValues = TRUE;
24+
2225
/** @var array */
2326
private $items = [];
2427

@@ -56,7 +59,7 @@ public function loadHttpData()
5659
*/
5760
public function setValue($value)
5861
{
59-
if ($value !== NULL && !array_key_exists((string) $value, $this->items)) {
62+
if ($this->checkAllowedValues && $value !== NULL && !array_key_exists((string) $value, $this->items)) {
6063
$set = Nette\Utils\Strings::truncate(implode(', ', array_map(function ($s) { return var_export($s, TRUE); }, array_keys($this->items))), 70, '...');
6164
throw new Nette\InvalidArgumentException("Value '$value' is out of allowed set [$set] in field '{$this->name}'.");
6265
}

src/Forms/Controls/MultiChoiceControl.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
*/
2020
abstract class MultiChoiceControl extends BaseControl
2121
{
22+
/** @var bool */
23+
public $checkAllowedValues = TRUE;
24+
2225
/** @var array */
2326
private $items = [];
2427

@@ -65,7 +68,7 @@ public function setValue($values)
6568
$flip[(string) $value] = TRUE;
6669
}
6770
$values = array_keys($flip);
68-
if ($diff = array_diff($values, array_keys($this->items))) {
71+
if ($this->checkAllowedValues && ($diff = array_diff($values, array_keys($this->items)))) {
6972
$set = Nette\Utils\Strings::truncate(implode(', ', array_map(function ($s) { return var_export($s, TRUE); }, array_keys($this->items))), 70, '...');
7073
$vals = (count($diff) > 1 ? 's' : '') . " '" . implode("', '", $diff) . "'";
7174
throw new Nette\InvalidArgumentException("Value$vals are out of allowed set [$set] in field '{$this->name}'.");

tests/Forms/Controls.ChoiceControl.loadData.phpt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ test(function () use ($series) { // setValue() and invalid argument
151151
});
152152

153153

154+
test(function () use ($series) { // setValue() and disabled $checkAllowedValues
155+
$form = new Form;
156+
$input = $form['select'] = new ChoiceControl(NULL, $series);
157+
$input->checkAllowedValues = FALSE;
158+
$input->setValue('unknown');
159+
Assert::null($input->getValue());
160+
});
161+
162+
154163
test(function () { // object as value
155164
$form = new Form;
156165
$input = $form['select'] = new ChoiceControl(NULL, ['2013-07-05 00:00:00' => 1]);

tests/Forms/Controls.MultiChoiceControl.loadData.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,23 @@ test(function () use ($series) { // setValue() and invalid argument
172172
});
173173

174174

175+
test(function () use ($series) { // setValue() and disabled $checkAllowedValues
176+
$form = new Form;
177+
$input = $form['select'] = new MultiChoiceControl(NULL, $series);
178+
$input->checkAllowedValues = FALSE;
179+
$input->setValue('unknown');
180+
Assert::same([], $input->getValue());
181+
182+
Assert::exception(function () use ($input) {
183+
$input->setValue(new stdClass);
184+
}, 'Nette\InvalidArgumentException', "Value must be array or NULL, object given in field 'select'.");
185+
186+
Assert::exception(function () use ($input) {
187+
$input->setValue([new stdClass]);
188+
}, 'Nette\InvalidArgumentException', "Values must be scalar, object given in field 'select'.");
189+
});
190+
191+
175192
test(function () { // object as value
176193
$form = new Form;
177194
$input = $form['select'] = new MultiChoiceControl(NULL, ['2013-07-05 00:00:00' => 1]);

0 commit comments

Comments
 (0)