Skip to content

Commit

Permalink
Validator::validatePattern(): fix value object support (fixes #205)
Browse files Browse the repository at this point in the history
  • Loading branch information
xificurk committed Dec 28, 2018
1 parent acf270f commit f3e84e6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Forms/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public static function validatePattern(IControl $control, string $pattern, bool
$regexp = "\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, $regexp)) {
if (!Strings::match((string) $value, $regexp)) {
return false;
}
}
Expand Down Expand Up @@ -353,6 +353,6 @@ public static function validateImage(Controls\UploadControl $control): bool

private static function toArray($value): array
{
return $value instanceof Nette\Http\FileUpload ? [$value] : (array) $value;
return is_object($value) ? [$value] : (array) $value;
}
}
8 changes: 8 additions & 0 deletions tests/Forms/Controls.TestBase.validators.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ test(function () {
Assert::false(Validator::validatePattern($control, '[0-9]+X'));
});

test(function () {
$control = new TextInput;
$control->value = new class () {public $lorem = 'ipsum'; public function __toString(): string {return '123x';}};
Assert::false(Validator::validatePattern($control, '[0-9]'));
Assert::true(Validator::validatePattern($control, '[0-9]+x'));
Assert::false(Validator::validatePattern($control, '[0-9]+X'));
});

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

0 comments on commit f3e84e6

Please sign in to comment.