Skip to content

Commit 9ea7e2e

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

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-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: 21 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,25 @@ test(function () {
9496
});
9597

9698

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

0 commit comments

Comments
 (0)