Skip to content

Commit 1a59835

Browse files
committed
PHPORM-68 Fix partial value un exist validator
1 parent 9f1f8f3 commit 1a59835

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/Validation/DatabasePresenceVerifier.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,13 @@ public function getCount($collection, $column, $value, $excludeId = null, $idCol
4343
*/
4444
public function getMultiCount($collection, $column, array $values, array $extra = [])
4545
{
46-
// Generates a regex like '/(a|b|c)/i' which can query multiple values
47-
$regex = '/('.implode('|', $values).')/i';
46+
// Nothing can match an empty array. Return early to avoid matching an empty string.
47+
if ($values === []) {
48+
return 0;
49+
}
50+
51+
// Generates a regex like '/^(a|b|c)$/i' which can query multiple values
52+
$regex = new Regex('^('.implode('|', $values).')$', 'i');
4853

4954
$query = $this->table($collection)->where($column, 'regex', $regex);
5055

tests/ValidationTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,19 @@ public function testExists(): void
103103
['name' => 'required|exists:users']
104104
);
105105
$this->assertFalse($validator->fails());
106+
107+
$validator = Validator::make(
108+
['name' => ['test name', 'john']], // Part of an existing value
109+
['name' => 'required|exists:users']
110+
);
111+
$this->assertTrue($validator->fails());
112+
113+
User::create(['name' => '']);
114+
115+
$validator = Validator::make(
116+
['name' => []],
117+
['name' => 'exists:users']
118+
);
119+
$this->assertFalse($validator->fails());
106120
}
107121
}

0 commit comments

Comments
 (0)