Skip to content

[Closed][Stale Code] Fix working of unique and exists #900

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/Jenssegers/Mongodb/Validation/DatabasePresenceVerifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php namespace Jenssegers\Mongodb\Validation;

class DatabasePresenceVerifier extends \Illuminate\Validation\DatabasePresenceVerifier
{
/**
* Count the number of objects in a collection having the given value.
*
* @param string $collection
* @param string $column
* @param string $value
* @param int $excludeId
* @param string $idColumn
* @param array $extra
* @return int
*/
public function getCount($collection, $column, $value, $excludeId = null, $idColumn = null, array $extra = [])
{
$query = $this->table($collection)->where($column, 'regex', "/$value/i");

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

foreach ($extra as $key => $extraValue) {
$this->addWhere($query, $key, $extraValue);
}

return $query->count();
}

/**
* Count the number of objects in a collection with the given values.
*
* @param string $collection
* @param string $column
* @param array $values
* @param array $extra
* @return int
*/
public function getMultiCount($collection, $column, array $values, array $extra = [])
{
foreach ($values as &$value) {
$value = new \MongoRegex("/$value/i");
}

$query = $this->table($collection)->whereIn($column, $values);

foreach ($extra as $key => $extraValue) {
$this->addWhere($query, $key, $extraValue);
}

return $query->count();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php namespace Jenssegers\Mongodb\Validation;

use Illuminate\Validation\ValidationServiceProvider;

class MongodbValidationServiceProvider extends ValidationServiceProvider
{
protected function registerPresenceVerifier()
{
$this->app->singleton('validation.presence', function ($app) {
return new DatabasePresenceVerifier($app['db']);
});
}
}
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ protected function getPackageProviders($app)
return [
'Jenssegers\Mongodb\MongodbServiceProvider',
'Jenssegers\Mongodb\Auth\PasswordResetServiceProvider',
'Jenssegers\Mongodb\Validation\MongodbValidationServiceProvider'
];
}

Expand Down
49 changes: 48 additions & 1 deletion tests/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

class ValidationTest extends TestCase
{

public function tearDown()
{
User::truncate();
Expand All @@ -23,5 +22,53 @@ public function testUnique()
['name' => 'required|unique:users']
);
$this->assertTrue($validator->fails());

$validator = Validator::make(
['name' => 'John doe'],
['name' => 'required|unique:users']
);
$this->assertTrue($validator->fails());

$validator = Validator::make(
['name' => 'john doe'],
['name' => 'required|unique:users']
);
$this->assertTrue($validator->fails());

$validator = Validator::make(
['name' => 'test doe'],
['name' => 'required|unique:users']
);
$this->assertFalse($validator->fails());
}

public function testExists()
{
$validator = Validator::make(
['name' => 'John Doe'],
['name' => 'required|exists:users']
);
$this->assertTrue($validator->fails());

User::create(['name' => 'John Doe']);
User::create(['name' => 'Test Name']);

$validator = Validator::make(
['name' => 'John Doe'],
['name' => 'required|exists:users']
);
$this->assertFalse($validator->fails());

$validator = Validator::make(
['name' => 'john Doe'],
['name' => 'required|exists:users']
);
$this->assertFalse($validator->fails());

$validator = Validator::make(
['name' => ['test name', 'john doe']],
['name' => 'required|exists:users']
);
$this->assertFalse($validator->fails());
}
}