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 / Save / Download PDF From Blade Template In PHP Laravel
Simple Way To Create Resourceful API Controller In Laravel
Search Engine Optimization Concepts
NGINX Security Best Practices & Optimization
GitHub Login With PHP Laravel Socialite
Why You Should Run Cronjobs In Laravel?
What Is Composer? How Does It Work? Useful Composer Commands And Usage
Comment And Like System Using Disqus
URL Redirects From Called Functions In Laravel
Test Your Local Developing Laravel Web Application From Phone Browser Without Any Software
Generate Fake Data In PHP With Faker
Proper Way To Validate MIME Type Of Files While Handling File Uploads In PHP
Client-Side Form Validation With Javascript
Laravel Last Executed Query In Plain SQL Statement For Debugging