Skip to content

Commit 3a46876

Browse files
authored
PHPORM-68 Fix unique validator when the validated value is part of an existing value (mongodb#21)
1 parent 2824dc4 commit 3a46876

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file.
1414
- Remove call to deprecated `Collection::count` for `countDocuments` [#18](https://github.com/GromNaN/laravel-mongodb-private/pull/18) by [@GromNaN](https://github.com/GromNaN).
1515
- Accept operators prefixed by `$` in `Query\Builder::orWhere` [#20](https://github.com/GromNaN/laravel-mongodb-private/pull/20) by [@GromNaN](https://github.com/GromNaN).
1616
- Remove `Query\Builder::whereAll($column, $values)`. Use `Query\Builder::where($column, 'all', $values)` instead. [#16](https://github.com/GromNaN/laravel-mongodb-private/pull/16) by [@GromNaN](https://github.com/GromNaN).
17+
- Fix validation of unique values when the validated value is found as part of an existing value. [#21](https://github.com/GromNaN/laravel-mongodb-private/pull/21) by [@GromNaN](https://github.com/GromNaN).
1718

1819
## [3.9.2] - 2022-09-01
1920

src/Validation/DatabasePresenceVerifier.php

+3-1
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

+6
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)