Skip to content

Commit

Permalink
ChoiceControl, MultiChoiceControl, HiddenField: added BackedEnum supp…
Browse files Browse the repository at this point in the history
…ort (#293)
  • Loading branch information
martinsedlacek authored and dg committed Mar 8, 2023
1 parent 0b19cf1 commit a326d5f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/Forms/Controls/ChoiceControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}'.");
Expand Down
2 changes: 2 additions & 0 deletions src/Forms/Controls/HiddenField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
4 changes: 3 additions & 1 deletion src/Forms/Controls/MultiChoiceControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
29 changes: 23 additions & 6 deletions tests/Forms/Forms.enum.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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'));
});

0 comments on commit a326d5f

Please sign in to comment.