diff --git a/src/Forms/Validator.php b/src/Forms/Validator.php index 758626ddc..bed08b42b 100644 --- a/src/Forms/Validator.php +++ b/src/Forms/Validator.php @@ -248,7 +248,8 @@ public static function validateUrl(IControl $control): bool */ public static function validatePattern(IControl $control, string $pattern): bool { - return (bool) Strings::match($control->getValue(), "\x01^(?:$pattern)\\z\x01u"); + $value = $control->getValue() instanceof Nette\Http\FileUpload ? $control->getValue()->getName() : $control->getValue(); + return (bool) Strings::match($value, "\x01^(?:$pattern)\\z\x01u"); } diff --git a/tests/Forms/Controls.TestBase.validators.phpt b/tests/Forms/Controls.TestBase.validators.phpt index 714721cae..29d3de63b 100644 --- a/tests/Forms/Controls.TestBase.validators.phpt +++ b/tests/Forms/Controls.TestBase.validators.phpt @@ -7,7 +7,9 @@ declare(strict_types=1); use Nette\Forms\Controls\TextInput; +use Nette\Forms\Controls\UploadControl; use Nette\Forms\Validator; +use Nette\Http\FileUpload; use Tester\Assert; @@ -94,6 +96,25 @@ test(function () { }); +test(function () { + class MockUploadControl extends UploadControl + { + public function setValue($value) + { + $this->value = $value; + return $this; + } + } + + $control = new MockUploadControl; + $control->value = new FileUpload([ + 'name' => '123x', 'size' => 1, 'tmp_name' => '456y', 'error' => UPLOAD_ERR_OK, 'type' => '', + ]); + Assert::false(Validator::validatePattern($control, '[4-6]+y')); + Assert::true(Validator::validatePattern($control, '[1-3]+x')); +}); + + test(function () { $control = new TextInput; $control->value = '';