Skip to content

Commit

Permalink
Validator: added support for enums (#282)
Browse files Browse the repository at this point in the history
Co-authored-by: martin.sedlacek <martin.sedlacek@motv.eu>
  • Loading branch information
2 people authored and dg committed Mar 29, 2022
1 parent 00047e8 commit 0781b1b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Forms/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ public static function validateEqual(Control $control, $arg): bool

foreach ($values as $val) {
foreach ($args as $item) {
if ($item instanceof \BackedEnum) {
$item = $item->value;
}

if ((string) $val === (string) $item) {
continue 2;
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Forms/Controls.BaseControl.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ use Tester\Assert;
require __DIR__ . '/../bootstrap.php';


enum TestEnum: string {
case CASE_1 = 'case 1';
case CASE_2 = 'case 2';
}


before(function () {
Form::initialize(true);
});
Expand Down Expand Up @@ -76,6 +82,18 @@ test('validators', function () {
});


test('validators for enums', function () {
$form = new Form;
$input = $form->addText('text');
$input->setValue(TestEnum::CASE_1->value);

Assert::true(Validator::validateEqual($input, TestEnum::CASE_1));
Assert::true(Validator::validateEqual($input, 'case 1'));
Assert::false(Validator::validateEqual($input, TestEnum::CASE_2));
Assert::false(Validator::validateEqual($input, 1));
});


test('validators for array', function () {
$form = new Form;
$input = $form->addMultiSelect('select', null, ['a', 'b', 'c', 'd']);
Expand Down

0 comments on commit 0781b1b

Please sign in to comment.