Skip to content
Merged
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
28 changes: 27 additions & 1 deletion src/DoctrineServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public function boot()
$this->getConfigPath() => config_path('doctrine.php'),
], 'config');
}

$this->ensureValidatorIsUsable();
}

/**
Expand Down Expand Up @@ -81,6 +83,26 @@ public function register()
}
}

protected function ensureValidatorIsUsable()
{
if(!$this->isLumen()) {
return;
}

if($this->shouldRegisterDoctrinePresenceValidator()) {
// due to weirdness the default presence verifier overrides one set by a service provider
// so remove them so we can re add our implementation later
unset($this->app->availableBindings['validator']);
unset($this->app->availableBindings['Illuminate\Contracts\Validation\Factory']);
} else {
// resolve the db,
// this makes `isset($this->app['db']) == true`
// which is required to set the presence verifier
// in the default ValidationServiceProvider implementation
$this->app['db'];
}
}

/**
* Merge config
*/
Expand Down Expand Up @@ -202,7 +224,11 @@ protected function registerExtensions()
*/
protected function registerPresenceVerifierProvider()
{
$this->app->register(PresenceVerifierProvider::class);
$this->app->singleton('validator', function () {
$this->app->register(PresenceVerifierProvider::class);

return $this->app->make('validator');
});
}

/**
Expand Down
22 changes: 22 additions & 0 deletions src/Validation/PresenceVerifierProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace LaravelDoctrine\ORM\Validation;

use Illuminate\Validation\Factory;
use Illuminate\Validation\ValidationServiceProvider;

class PresenceVerifierProvider extends ValidationServiceProvider
Expand All @@ -13,6 +14,27 @@ class PresenceVerifierProvider extends ValidationServiceProvider
*/
protected $defer = true;

/**
* Register the validation factory.
*
* @return void
*/
protected function registerValidationFactory()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to redeclare this if we are extending ValidationServiceProvider ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the default implementation checks for different unrelated conditions before calling $validator->setPresenceVerifier

{
$this->app->singleton('validator', function ($app) {
$validator = new Factory($app['translator'], $app);

// The validation presence verifier is responsible for determining the existence of
// values in a given data collection which is typically a relational database or
// other persistent data stores. It is used to check for "uniqueness" as well.
if (isset($app['registry']) && isset($app['validation.presence'])) {
$validator->setPresenceVerifier($app['validation.presence']);
}

return $validator;
});
}

/**
* Register the database presence verifier.
*
Expand Down