Skip to content
This repository was archived by the owner on Aug 22, 2023. It is now read-only.

Commit 7c96086

Browse files
committed
PHPORM-68 Fix unique validator when the validated value is part of an existing value
1 parent 2824dc4 commit 7c96086

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/Validation/DatabasePresenceVerifier.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Jenssegers\Mongodb\Validation;
44

5+
use MongoDB\BSON\Regex;
6+
57
class DatabasePresenceVerifier extends \Illuminate\Validation\DatabasePresenceVerifier
68
{
79
/**
@@ -17,7 +19,7 @@ class DatabasePresenceVerifier extends \Illuminate\Validation\DatabasePresenceVe
1719
*/
1820
public function getCount($collection, $column, $value, $excludeId = null, $idColumn = null, array $extra = [])
1921
{
20-
$query = $this->table($collection)->where($column, 'regex', '/'.preg_quote($value).'/i');
22+
$query = $this->table($collection)->where($column, new Regex('^'.preg_quote($value).'$', '/i'));
2123

2224
if ($excludeId !== null && $excludeId != 'NULL') {
2325
$query->where($idColumn ?: 'id', '<>', $excludeId);

tests/ValidationTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ public function testUnique(): void
4848
);
4949
$this->assertFalse($validator->fails());
5050

51+
$validator = Validator::make(
52+
['name' => 'John'], // Part of an existing value
53+
['name' => 'required|unique:users']
54+
);
55+
$this->assertFalse($validator->fails());
56+
5157
User::create(['name' => 'Johnny Cash', 'email' => '[email protected]']);
5258

5359
$validator = Validator::make(

0 commit comments

Comments
 (0)