Skip to content

Commit 29944fd

Browse files
h4kunadg
authored andcommitted
Validator: method validatePattern() for FileUpload uses file name (#175)
1 parent 48fd2c1 commit 29944fd

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/Forms/Validator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ public static function validateUrl(IControl $control): bool
248248
*/
249249
public static function validatePattern(IControl $control, string $pattern): bool
250250
{
251-
return (bool) Strings::match($control->getValue(), "\x01^(?:$pattern)\\z\x01u");
251+
$value = $control->getValue() instanceof Nette\Http\FileUpload ? $control->getValue()->getName() : $control->getValue();
252+
return (bool) Strings::match($value, "\x01^(?:$pattern)\\z\x01u");
252253
}
253254

254255

tests/Forms/Controls.TestBase.validators.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
declare(strict_types=1);
88

99
use Nette\Forms\Controls\TextInput;
10+
use Nette\Forms\Controls\UploadControl;
1011
use Nette\Forms\Validator;
12+
use Nette\Http\FileUpload;
1113
use Tester\Assert;
1214

1315

@@ -91,6 +93,21 @@ test(function () {
9193
Assert::false(Validator::validatePattern($control, '[0-9]'));
9294
Assert::true(Validator::validatePattern($control, '[0-9]+x'));
9395
Assert::false(Validator::validatePattern($control, '[0-9]+X'));
96+
97+
98+
$control = new class extends UploadControl {
99+
public function setValue($value)
100+
{
101+
$this->value = $value;
102+
return $this;
103+
}
104+
};
105+
106+
$control->value = new FileUpload([
107+
'name' => '123x', 'size' => 1, 'tmp_name' => '456y', 'error' => UPLOAD_ERR_OK, 'type' => '',
108+
]);
109+
Assert::false(Validator::validatePattern($control, '[4-6]+y'));
110+
Assert::true(Validator::validatePattern($control, '[1-3]+x'));
94111
});
95112

96113

0 commit comments

Comments
 (0)