Skip to content

Commit d6af1d1

Browse files
h4kunadg
authored andcommitted
Validator: method validatePattern() for FileUpload uses file name (#175)
1 parent 6dc7a01 commit d6af1d1

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
@@ -274,7 +274,8 @@ public static function validateUrl(IControl $control)
274274
*/
275275
public static function validatePattern(IControl $control, $pattern)
276276
{
277-
return (bool) Strings::match($control->getValue(), "\x01^(?:$pattern)\\z\x01u");
277+
$value = $control->getValue() instanceof Nette\Http\FileUpload ? $control->getValue()->getName() : $control->getValue();
278+
return (bool) Strings::match($value, "\x01^(?:$pattern)\\z\x01u");
278279
}
279280

280281

tests/Forms/Controls.TestBase.validators.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
*/
66

77
use Nette\Forms\Controls\TextInput;
8+
use Nette\Forms\Controls\UploadControl;
89
use Nette\Forms\Validator;
10+
use Nette\Http\FileUpload;
911
use Tester\Assert;
1012

1113

@@ -92,6 +94,25 @@ test(function () {
9294
});
9395

9496

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

0 commit comments

Comments
 (0)