From 9819f4b672ab98b5566cf3367ce94ebefd12ff84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Mat=C4=9Bj=C4=8Dek?= Date: Sun, 25 Feb 2018 17:14:55 +0100 Subject: [PATCH] Validator: method validatePattern() for FileUpload uses file name (#175) --- src/Forms/Validator.php | 3 ++- tests/Forms/Controls.TestBase.validators.phpt | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Forms/Validator.php b/src/Forms/Validator.php index 5a725338f..b06137f6b 100644 --- a/src/Forms/Validator.php +++ b/src/Forms/Validator.php @@ -274,7 +274,8 @@ public static function validateUrl(IControl $control) */ public static function validatePattern(IControl $control, $pattern) { - 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 390200c56..34a9156a1 100644 --- a/tests/Forms/Controls.TestBase.validators.phpt +++ b/tests/Forms/Controls.TestBase.validators.phpt @@ -5,7 +5,9 @@ */ use Nette\Forms\Controls\TextInput; +use Nette\Forms\Controls\UploadControl; use Nette\Forms\Validator; +use Nette\Http\FileUpload; use Tester\Assert; @@ -92,6 +94,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 = '';