From 971e4e37149c033cffd6a35367c98d6dc9a9f6c4 Mon Sep 17 00:00:00 2001 From: Matthew Nessworthy Date: Thu, 8 Mar 2018 12:37:46 +0100 Subject: [PATCH] Add custom validation rule during validation resolving https://github.com/greggilbert/recaptcha/pull/86 --- src/RecaptchaServiceProvider.php | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/RecaptchaServiceProvider.php b/src/RecaptchaServiceProvider.php index 5571096..d7d6c03 100644 --- a/src/RecaptchaServiceProvider.php +++ b/src/RecaptchaServiceProvider.php @@ -25,8 +25,6 @@ class RecaptchaServiceProvider extends ServiceProvider */ public function boot() { - $this->addValidator(); - $this->loadViewsFrom(__DIR__ . '/views', 'recaptcha'); } @@ -35,12 +33,21 @@ public function boot() */ public function addValidator() { - $this->app->validator->extendImplicit('recaptcha', function ($attribute, $value, $parameters) { - $captcha = app('recaptcha.service'); - $challenge = app('request')->input($captcha->getResponseKey()); - - return $captcha->check($challenge, $value); - }, 'Please ensure that you are a human!'); + $this->app->resolving( + 'validator', + function ($validator) { + $validator->extendImplicit( + 'recaptcha', + function ($attribute, $value, $parameters) { + $captcha = app('recaptcha.service'); + $challenge = app('request')->input($captcha->getResponseKey()); + + return $captcha->check($challenge, $value); + }, + 'Please ensure that you are a human!' + ); + } + ); } /** @@ -52,6 +59,7 @@ public function register() { $this->handleConfig(); $this->bindRecaptcha(); + $this->addValidator(); } protected function bindRecaptcha() @@ -81,7 +89,7 @@ protected function bindRecaptcha() protected function handleConfig() { - $packageConfig = __DIR__ . '/config/recaptcha.php'; + $packageConfig = __DIR__ . '/config/recaptcha.php'; $destinationConfig = config_path('recaptcha.php'); $this->publishes([