Skip to content

Commit 52e0d4f

Browse files
xificurkdg
authored andcommitted
Validator::validatePattern(): fix value object support (fixes #205)
1 parent 0a10145 commit 52e0d4f

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/Forms/Validator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public static function validatePattern(IControl $control, string $pattern, bool
256256
$regexp = "\x01^(?:$pattern)\\z\x01u" . ($caseInsensitive ? 'i' : '');
257257
foreach (static::toArray($control->getValue()) as $item) {
258258
$value = $item instanceof Nette\Http\FileUpload ? $item->getName() : $item;
259-
if (!Strings::match($value, $regexp)) {
259+
if (!Strings::match((string) $value, $regexp)) {
260260
return false;
261261
}
262262
}
@@ -355,6 +355,6 @@ public static function validateImage(Controls\UploadControl $control): bool
355355

356356
private static function toArray($value): array
357357
{
358-
return $value instanceof Nette\Http\FileUpload ? [$value] : (array) $value;
358+
return is_object($value) ? [$value] : (array) $value;
359359
}
360360
}

tests/Forms/Controls.TestBase.validators.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,26 @@ test(function () {
9595
Assert::false(Validator::validatePattern($control, '[0-9]+X'));
9696
});
9797

98+
test(function () {
99+
$control = new TextInput;
100+
101+
102+
$control->value = new class () {
103+
public $lorem = 'ipsum';
104+
105+
106+
public function __toString(): string
107+
{
108+
return '123x';
109+
}
110+
};
111+
112+
113+
Assert::false(Validator::validatePattern($control, '[0-9]'));
114+
Assert::true(Validator::validatePattern($control, '[0-9]+x'));
115+
Assert::false(Validator::validatePattern($control, '[0-9]+X'));
116+
});
117+
98118
test(function () {
99119
$control = new TextInput;
100120
$control->value = '123x';

0 commit comments

Comments
 (0)