Skip to content

Commit 4cb2a2e

Browse files
h4kunadg
authored andcommitted
Validator: method validatePattern() for UploadControl multiple (#192)
1 parent c2d4d93 commit 4cb2a2e

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/Forms/Validator.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,14 @@ public static function validateUrl(IControl $control): bool
251251
*/
252252
public static function validatePattern(IControl $control, string $pattern, bool $caseInsensitive = false): bool
253253
{
254-
$value = $control->getValue() instanceof Nette\Http\FileUpload ? $control->getValue()->getName() : $control->getValue();
255-
return (bool) Strings::match($value, "\x01^(?:$pattern)\\z\x01u" . ($caseInsensitive ? 'i' : ''));
254+
$regexp = "\x01^(?:$pattern)\\z\x01u" . ($caseInsensitive ? 'i' : '');
255+
foreach (static::toArray($control->getValue()) as $item) {
256+
$value = $item instanceof Nette\Http\FileUpload ? $item->getName() : $item;
257+
if (!Strings::match($value, $regexp)) {
258+
return false;
259+
}
260+
}
261+
return true;
256262
}
257263

258264

tests/Forms/Controls.TestBase.validators.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,18 @@ test(function () {
120120
]);
121121
Assert::false(Validator::validatePattern($control, '[4-6]+y'));
122122
Assert::true(Validator::validatePattern($control, '[1-3]+x'));
123+
124+
$control = new MockUploadControl(null, true);
125+
$control->value = [new FileUpload([
126+
'name' => 'foo.jpg', 'size' => 1, 'tmp_name' => 'foo1', 'error' => UPLOAD_ERR_OK, 'type' => '',
127+
])];
128+
Assert::true(Validator::validatePattern($control, '.*jpg'));
129+
130+
$control = new MockUploadControl(null, true);
131+
$control->value = [new FileUpload([
132+
'name' => 'bar.png', 'size' => 1, 'tmp_name' => 'bar1', 'error' => UPLOAD_ERR_OK, 'type' => '',
133+
])];
134+
Assert::false(Validator::validatePattern($control, '.*jpg'));
123135
});
124136

125137

0 commit comments

Comments
 (0)