
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.
Global Data In All Laravel Blade Pages
Laravel Custom Maintenance Page
Client-Side DataTable, Adding Super Powers To HTML Table
Debugging Laravel Queue Email Issues
Generate Sitemap in PHP Laravel
Google, Twitter, GitHub, Facebook & Many Other Social Generic Logins With PHP Laravel Socialite
Make Laravel Controllers Slim By Skimming Form Validation Request
PHP Built-In Web Server & Testing Your Development Project In Mobile Without Any Software
Stripe Payment Integration With Laravel
Accessors And Mutators In PHP Laravel
Foreign Key Migrations Simplified In Laravel 7.x