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 authored and dg committed Feb 28, 2019
1 parent 0a10145 commit 52e0d4f
Show file tree
Hide file tree
Showing 2 changed files with 22 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 @@ -256,7 +256,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 @@ -355,6 +355,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;
}
}
20 changes: 20 additions & 0 deletions tests/Forms/Controls.TestBase.validators.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,26 @@ 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 52e0d4f

Please sign in to comment.