
Good content takes time and effort to come up with.
Please consider supporting us by just disabling your AD BLOCKER and reloading this page again.
Yo! Whats up guys. Today you will learn how to create custom Laravel
validation class and use it in a real example.
Sometimes there might arise the requirement to create your own validation rules and start flying out of the Laravel
builtin validation nest.
You will learn how to do Google reCAPTCHA custom server side validation class with Laravel
Validator.
Read how to create Google reCAPTCHA with Laravel form, not sure where ? Then I have an entire article on it, please feel free to read it How To Do Google reCAPTCHA Integration In PHP Laravel Forms.
Run the following command to create GoogleRecaptcha
validation rule class
php artisan make:rule GoogleRecaptcha
This will create GoogleRecaptcha
validation rule class inside App/Rules
folder
When you open App/Rules/GoogleRecaptcha
file. Then you will see the following bare bone code in it.
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
use ReCaptcha\ReCaptcha;
class GoogleRecaptcha implements Rule
{
public function __construct()
{
//
}
public function passes($attribute, $value)
{
}
public function message()
{
}
}
In above rule I didn't need any extra variables for usage in this rule class so __construct
is empty.
You can pass variables through the __contruct
function while calling GoogleRecaptcha
rule. Like the following
ContactsController
request()->validate([
/** See this line this is where google recaptcha will be validated */
'g-recaptcha-response' => new GoogleRecaptcha($someVariable),
]);
GoogleRecaptcha Class
class GoogleRecaptcha implments Rule{
protected $someVariable;
public function __construct($someVariable){
$this->someVariable = $someVariable
}
public function passes($attribute, $value){
/** SomeVariable can be access here like the following **/
// $this->someVariable
}
}
By default Laravel
passes form attribute ($attribute) and its value ($value) in passes
method as seen in the code.
public function passes($attribute, $value)
public function message()
When the validation fails you can pass the custom messages from this function.
When you add google validation then you will have the following code. I have removed comments just for the sake of demonstration. Make sure to include those.
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
use ReCaptcha\ReCaptcha;
class GoogleRecaptcha implements Rule
{
public function __construct()
{
//
}
public function passes($attribute, $value)
{
$recaptcha = new ReCaptcha(env('RECAPTCHA_SECRET_KEY'));
$response = $recaptcha->verify($value, $_SERVER['REMOTE_ADDR']);
return $response->isSuccess();
}
public function message()
{
return 'Please verify recaptcha.';
}
}
For the sake of demo I am adding my validation in controller, proper way to add validation rules is in Requests Class
to make your controller slim.
public function store()
{
request()->validate([
/** Other field validation exists, for the demo I have removed those */
'g-recaptcha-response' => new GoogleRecaptcha(),
]);
/** Store Validate Data */
}
You learnt how to do added custom validation rules. So go ahead and implement them in your projects.
Create Zip File On The Fly With Streaming Download In PHP Laravel
Multiple File Uploads In Laravel PHP
Securely SSH Your Server & Push Files With FileZilla
@stack @push and @prepend In Laravel Blade
Send SMS With Amazon SNS (Simple Notification Service) Using PHP
Composer Install v/s Composer Update
Install NGINX In Linux / Ubuntu And Managing
Client-Side DataTable, Adding Super Powers To HTML Table
Website Speed and Performance Optimizations
Simple Way To Create Resourceful API Controller In Laravel
Create / Save / Download PDF From Blade Template In PHP Laravel
Generate RSS Feeds in PHP Laravel