Skip to content

Commit

Permalink
Validator: method validatePattern() for FileUpload uses file name (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna authored and dg committed Feb 25, 2018
1 parent 48fd2c1 commit 5f5aec3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Forms/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ public static function validateUrl(IControl $control): bool
*/
public static function validatePattern(IControl $control, string $pattern): bool
{
return (bool) Strings::match($control->getValue(), "\x01^(?:$pattern)\\z\x01u");
$value = $control->getValue() instanceof Nette\Http\FileUpload ? $control->getValue()->getName() : $control->getValue();
return (bool) Strings::match($value, "\x01^(?:$pattern)\\z\x01u");
}


Expand Down
20 changes: 20 additions & 0 deletions tests/Forms/Controls.TestBase.validators.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
declare(strict_types=1);

use Nette\Forms\Controls\TextInput;
use Nette\Forms\Controls\UploadControl;
use Nette\Forms\Validator;
use Nette\Http\FileUpload;
use Tester\Assert;


Expand Down Expand Up @@ -94,6 +96,24 @@ test(function () {
});


test(function () {
class MockUploadControl extends UploadControl {
public function setValue($value)
{
$this->value = $value;
return $this;
}
};

$control = new MockUploadControl;
$control->value = new FileUpload([
'name' => '123x', 'size' => 1, 'tmp_name' => '456y', 'error' => UPLOAD_ERR_OK, 'type' => '',
]);
Assert::false(Validator::validatePattern($control, '[4-6]+y'));
Assert::true(Validator::validatePattern($control, '[1-3]+x'));
});


test(function () {
$control = new TextInput;
$control->value = '';
Expand Down

0 comments on commit 5f5aec3

Please sign in to comment.