Skip to content

Commit 142876d

Browse files
authored
Merge pull request #2153 from divine/add_escape_3_6
[3.6] Backport of #1992 to 3.6
2 parents f76732c + 97b0ed2 commit 142876d

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ All notable changes to this project will be documented in this file.
77

88
### Changed
99
- MongodbQueueServiceProvider does not use the DB Facade anymore [#2149](https://github.com/jenssegers/laravel-mongodb/pull/2149) by [@curosmj](https://github.com/curosmj)
10+
- Add escape regex chars to DB Presence Verifier [#1992](https://github.com/jenssegers/laravel-mongodb/pull/1992) by [@andrei-gafton-rtgt](https://github.com/andrei-gafton-rtgt).
11+
1012

1113
## [3.6.6] - 2020-10-29
1214

src/Jenssegers/Mongodb/Validation/DatabasePresenceVerifier.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class DatabasePresenceVerifier extends \Illuminate\Validation\DatabasePresenceVe
1616
*/
1717
public function getCount($collection, $column, $value, $excludeId = null, $idColumn = null, array $extra = [])
1818
{
19-
$query = $this->table($collection)->where($column, 'regex', "/$value/i");
19+
$query = $this->table($collection)->where($column, 'regex', "/" . preg_quote($value) . "/i");
2020

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

tests/ValidationTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,26 @@ public function testUnique(): void
4141
['name' => 'required|unique:users']
4242
);
4343
$this->assertFalse($validator->fails());
44+
45+
User::create(['name' => 'Johnny Cash', 'email' => '[email protected]']);
46+
47+
$validator = Validator::make(
48+
['email' => '[email protected]'],
49+
['email' => 'required|unique:users']
50+
);
51+
$this->assertTrue($validator->fails());
52+
53+
$validator = Validator::make(
54+
['email' => '[email protected]'],
55+
['email' => 'required|unique:users']
56+
);
57+
$this->assertFalse($validator->fails());
58+
59+
$validator = Validator::make(
60+
['email' => '[email protected]'],
61+
['email' => 'required|unique:users']
62+
);
63+
$this->assertFalse($validator->fails());
4464
}
4565

4666
public function testExists(): void

tests/models/User.php

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* Class User
1414
* @property string $_id
1515
* @property string $name
16+
* @property string $email
1617
* @property string $title
1718
* @property int $age
1819
* @property \Carbon\Carbon $birthday

0 commit comments

Comments
 (0)