diff --git a/src/Forms/Validator.php b/src/Forms/Validator.php index 136c19fb6..0b0436a68 100644 --- a/src/Forms/Validator.php +++ b/src/Forms/Validator.php @@ -256,7 +256,7 @@ public static function validatePattern(IControl $control, string $pattern, bool $regexp = "\x01^(?:$pattern)\\z\x01u" . ($caseInsensitive ? 'i' : ''); foreach (static::toArray($control->getValue()) as $item) { $value = $item instanceof Nette\Http\FileUpload ? $item->getName() : $item; - if (!Strings::match($value, $regexp)) { + if (!Strings::match((string) $value, $regexp)) { return false; } } @@ -355,6 +355,6 @@ public static function validateImage(Controls\UploadControl $control): bool private static function toArray($value): array { - return $value instanceof Nette\Http\FileUpload ? [$value] : (array) $value; + return is_object($value) ? [$value] : (array) $value; } } diff --git a/tests/Forms/Controls.TestBase.validators.phpt b/tests/Forms/Controls.TestBase.validators.phpt index 3eafd1f0b..43c2516f9 100644 --- a/tests/Forms/Controls.TestBase.validators.phpt +++ b/tests/Forms/Controls.TestBase.validators.phpt @@ -95,6 +95,26 @@ test(function () { Assert::false(Validator::validatePattern($control, '[0-9]+X')); }); +test(function () { + $control = new TextInput; + + + $control->value = new class () { + public $lorem = 'ipsum'; + + + public function __toString(): string + { + return '123x'; + } + }; + + + Assert::false(Validator::validatePattern($control, '[0-9]')); + Assert::true(Validator::validatePattern($control, '[0-9]+x')); + Assert::false(Validator::validatePattern($control, '[0-9]+X')); +}); + test(function () { $control = new TextInput; $control->value = '123x';