How to Add Captcha code validate and refresh captcha in Laravel 9 Form?
Hi Dev,
Here, I will show you how to works How to Add Captcha code validate and refresh captcha in Laravel 9 Form?. it's simple example of How to Create Captcha Code using Bootdetect Package in Laravel 9 . you will learn Laravel 9 Word Captcha Code and Validation Example. In this article, we will implement a Laravel 9 Captcha Tutorial.
Let's get started with How to Generate Captcha Code in Laravel 9.
This article will give you simple Laravel 9 Generate Captcha Code using Bootdetect Package Example
Step 1: Download LaravelLet us begin the tutorial by installing a new laravel application. if you have already created the project, then skip the following step.
composer create-project laravel/laravel example-appStep 2: Install Captcha package
In first step we will install captcha-com/laravel-captcha package for generate captcha code image. this package through we can generate captcha code image for our project. so first fire bellow command in your cmd or terminal:
composer require captcha-com/laravel-captcha:"4.*"
Now we need to add provider path and alias path in config/app.php file so open that file and add bellow code.
config/app.phpreturn [ $provides => [ LaravelCaptcha\Providers\LaravelCaptchaServiceProvider::class ],
Now we will run bellow command that way it will generate app/captcha.php file for configration and we can change and customize easily.
php artisan vendor:publishStep 3: Create Controller
We will use Laravel default auth mechanism to complete our project. So we have no need to create new controller. In your register controller your default validator function will be look like this.
app/Http/Controllers/Auth/RegisterController.phpprotected 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', 'CaptchaCode' => 'required|valid_captcha' ]); }
Look we just added 'CaptchaCode' => 'required|valid_captcha' line to get Capctcha code image.
Step 4: Create Blade FileThis 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<div class="form-group{{ $errors->has('CaptchaCode') ? ' has-error' : '' }}"> <label class="col-md-4 control-label">Captcha</label> <div class="col-md-6"> {!! captcha_image_html('ContactCaptcha') !!} <input class="form-control" type="text" id="CaptchaCode" name="CaptchaCode"> @if ($errors->has('CaptchaCode')) <span class="help-block"> <strong>{{ $errors->first('CaptchaCode') }}</strong> </span> @endif </div> </div>
It will help you...