Skip to content

Commit

Permalink
Validator: method validatePattern() for UploadControl multiple (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna authored and dg committed Sep 30, 2018
1 parent c2d4d93 commit a2da720
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Forms/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,14 @@ public static function validateUrl(IControl $control): bool
*/
public static function validatePattern(IControl $control, string $pattern, bool $caseInsensitive = false): bool
{
$value = $control->getValue() instanceof Nette\Http\FileUpload ? $control->getValue()->getName() : $control->getValue();
return (bool) Strings::match($value, "\x01^(?:$pattern)\\z\x01u" . ($caseInsensitive ? 'i' : ''));
$matchPattern = "\x01^(?:$pattern)\\z\x01u" . ($caseInsensitive ? 'i' : '');
foreach (static::toArray($control->getValue()) as $item) {
$value = $item instanceof Nette\Http\FileUpload ? $item->getName() : $item;
if (Strings::match($value, $matchPattern) === null) {
return false;
}
}
return true;
}


Expand Down
12 changes: 12 additions & 0 deletions tests/Forms/Controls.TestBase.validators.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ test(function () {
]);
Assert::false(Validator::validatePattern($control, '[4-6]+y'));
Assert::true(Validator::validatePattern($control, '[1-3]+x'));

$control = new MockUploadControl(null, true);
$control->value = [new FileUpload([
'name' => 'foo.jpg', 'size' => 1, 'tmp_name' => 'foo1', 'error' => UPLOAD_ERR_OK, 'type' => '',
])];
Assert::true(Validator::validatePattern($control, '.*jpg'));

$control = new MockUploadControl(null, true);
$control->value = [new FileUpload([
'name' => 'bar.png', 'size' => 1, 'tmp_name' => 'bar1', 'error' => UPLOAD_ERR_OK, 'type' => '',
])];
Assert::false(Validator::validatePattern($control, '.*jpg'));
});


Expand Down
2 changes: 2 additions & 0 deletions tests/Forms/Forms.idMask.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

declare(strict_types=1);


use Nette\Forms\Controls\TextInput;
use Nette\Forms\Form;

use Tester\Assert;


Expand Down

0 comments on commit a2da720

Please sign in to comment.