From a326d5f52d67c11d33561f8501509d41a4212112 Mon Sep 17 00:00:00 2001 From: martinsedlacek Date: Mon, 2 Jan 2023 13:01:16 +0100 Subject: [PATCH] ChoiceControl, MultiChoiceControl, HiddenField: added BackedEnum support (#293) --- src/Forms/Controls/ChoiceControl.php | 6 ++++- src/Forms/Controls/HiddenField.php | 2 ++ src/Forms/Controls/MultiChoiceControl.php | 4 +++- tests/Forms/Forms.enum.phpt | 29 ++++++++++++++++++----- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/Forms/Controls/ChoiceControl.php b/src/Forms/Controls/ChoiceControl.php index 8c7e6b81b..6b17c4cdc 100644 --- a/src/Forms/Controls/ChoiceControl.php +++ b/src/Forms/Controls/ChoiceControl.php @@ -49,12 +49,16 @@ public function loadHttpData(): void /** * Sets selected item (by key). - * @param string|int|null $value + * @param string|int|\BackedEnum|null $value * @return static * @internal */ public function setValue($value) { + if ($value instanceof \BackedEnum) { + $value = $value->value; + } + if ($this->checkDefaultValue && $value !== null && !array_key_exists((string) $value, $this->items)) { $set = Nette\Utils\Strings::truncate(implode(', ', array_map(function ($s) { return var_export($s, true); }, array_keys($this->items))), 70, '...'); throw new Nette\InvalidArgumentException("Value '$value' is out of allowed set [$set] in field '{$this->name}'."); diff --git a/src/Forms/Controls/HiddenField.php b/src/Forms/Controls/HiddenField.php index d85597095..93b22b979 100644 --- a/src/Forms/Controls/HiddenField.php +++ b/src/Forms/Controls/HiddenField.php @@ -46,6 +46,8 @@ public function setValue($value) { if ($value === null) { $value = ''; + } elseif ($value instanceof \BackedEnum) { + $value = $value->value; } elseif (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) { throw new Nette\InvalidArgumentException(sprintf("Value must be scalar or null, %s given in field '%s'.", gettype($value), $this->name)); } diff --git a/src/Forms/Controls/MultiChoiceControl.php b/src/Forms/Controls/MultiChoiceControl.php index d7541f15a..7c8bbddda 100644 --- a/src/Forms/Controls/MultiChoiceControl.php +++ b/src/Forms/Controls/MultiChoiceControl.php @@ -60,7 +60,9 @@ public function setValue($values) $flip = []; foreach ($values as $value) { - if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) { + if ($value instanceof \BackedEnum) { + $value = $value->value; + } elseif (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) { throw new Nette\InvalidArgumentException(sprintf("Values must be scalar, %s given in field '%s'.", gettype($value), $this->name)); } diff --git a/tests/Forms/Forms.enum.phpt b/tests/Forms/Forms.enum.phpt index 02e79ce68..5e29562bc 100644 --- a/tests/Forms/Forms.enum.phpt +++ b/tests/Forms/Forms.enum.phpt @@ -21,13 +21,8 @@ enum TestEnum: string } -setUp(function () { - ob_start(); - Form::initialize(true); -}); - - test('validators for enums', function () { + Form::initialize(true); $form = new Form; $input = $form->addText('text'); $input->setValue(TestEnum::Case1->value); @@ -37,3 +32,25 @@ test('validators for enums', function () { Assert::false(Validator::validateEqual($input, TestEnum::Case2)); Assert::false(Validator::validateEqual($input, 1)); }); + + +test('enum as default value', function () { + $items = ['case 1' => '1', 'case 2' => '2', 'case 3' => '3', 'case 4' => '4']; + + Form::initialize(true); + $form = new Form; + $form->addSelect('select', null, $items); + $form->addMultiSelect('multi', null, $items); + $form->addHidden('hidden', TestEnum::Case2); + + $form->setDefaults([ + 'select' => TestEnum::Case1, + 'multi' => [TestEnum::Case1, TestEnum::Case2], + ]); + + Assert::same([ + 'select' => 'case 1', + 'multi' => ['case 1', 'case 2'], + 'hidden' => 'case 2', + ], $form->getValues('array')); +});