Skip to content

Commit 5f5aec3

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

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-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: 20 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

@@ -94,6 +96,24 @@ test(function () {
9496
});
9597

9698

99+
test(function () {
100+
class MockUploadControl extends UploadControl {
101+
public function setValue($value)
102+
{
103+
$this->value = $value;
104+
return $this;
105+
}
106+
};
107+
108+
$control = new MockUploadControl;
109+
$control->value = new FileUpload([
110+
'name' => '123x', 'size' => 1, 'tmp_name' => '456y', 'error' => UPLOAD_ERR_OK, 'type' => '',
111+
]);
112+
Assert::false(Validator::validatePattern($control, '[4-6]+y'));
113+
Assert::true(Validator::validatePattern($control, '[1-3]+x'));
114+
});
115+
116+
97117
test(function () {
98118
$control = new TextInput;
99119
$control->value = '';

0 commit comments

Comments
 (0)