Laravel 7.x Form Validation with Google ReCAPTCHA

Hello Artisan

Hope you are doing well, In this tutorial i will discuss about google recaptcha. That mean i will show you how we can integrate google recaptcha code in our laravel application.

Google ReCaptcha is a captcha like system, that provide security against hackers and sticks or curl requests. It assures that a computer user is a human. It is the best and most used captcha system available where users are only required to click on a checkbox and in some cases select some similar images related to conman question.

We can do it using anhskohbo/no-captcha packages. You can use other packages to do google recaptcha in your laravel app. But i am using anhskohbo/no-captch. 

We use google recaptcha to increase our website security. We can check human or robot using google recaptcha. So let's do google recaptcha code with valdation in laravel 7.

 

Also Read: Captcha Code and Validation Example using Botdetect Package in Laravel

 

In this example, we will create simple registration form and will try to create google captcha code. before use google captcha code we will install "anhskohbo/no-captcha" composer package for google captcha in our application. You have to just follow few step and you will get google re-captcha code in your laravel 6 application.

 

Step 1 : Download Laravel 7

first of all we need to get fresh Laravel 7 version application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project --prefer-dist laravel/laravel blog

 

Step 2: Install anhskohbo/no-captcha Package

In this step we need to install anhskohbo/no-captcha via the Composer package manager, so one your terminal and fire bellow command

composer require anhskohbo/no-captcha

 

After successfully install package, we require to add aliases and service provider.

Config/app.php

 [
         Anhskohbo\NoCaptcha\NoCaptchaServiceProvider::class
     ],

     'aliases' => [
         'NoCaptcha' => Anhskohbo\NoCaptcha\Facades\NoCaptcha::class,
     ]
  ]

 

Step 3: Update Google API Key

In this step we need to set google site key and secret key. If you don't have site key and secret key then you can create from here. First click on this link : Recaptcha Admin

After click you can see bellow view and you need register your site link this way:

recaptcha-code-with-validation

Now open .env file and add this two variable

.env

NOCAPTCHA_SITEKEY=[site-key]
NOCAPTCHA_SECRET=[secret-key]

 

Step 4: Add Route

We will use Laravel default auth mechanism to complete our project. So we have no need to create new controller. So go to your app/Http/Controllers/Auth/RegisterController.php. In your register controller your default validator function will be look like this. 

 app/Http/Controllers/Auth/RegisterController.php

 protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'slug' => 'required',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|min:6|dumbpwd|confirmed'
        ]);
    }

 

Now you have to just add one line code. see the below code

 protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'slug' => 'required',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|min:6|dumbpwd|confirmed',
            'g-recaptcha-response' => 'required|captcha',
        ]);
    }

 

Look we just added 'g-recaptcha-response' => 'required|captcha' line to get Capctcha code. 

 

Step 5: Create View File

This is the last step. So go to your resources/views/auth/register.blade.php and add the following code to your register form.

 resources/views/auth/register.blade.php

 

Finally, our google recaptcha form validdattion in laravel 7 is over. Hope it can help you.