![Theme Switcher Image](https://stackcoder.in/images/theme-switcher.png?cb=stackcoder90070)
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.
Use Different PHP Versions In Ubuntu / Linux
Simple Way To Create Resourceful API Controller In Laravel
Free SSL Certificate With Lets Encrypt/Certbot In Linux (Single / Multiple Domains)
Create Custom 404 Page In Laravel
Automate Repeating Tasks In Linux Server With Cronjobs
Factories To Speed Up Test-Driven Development In Laravel
Stripe Payment Integration With Laravel
Send Email In PHP With PHPMailer
Make Laravel Controllers Slim By Skimming Form Validation Request
Redirect www to a non-www Website Or Vice Versa
Supervisor For Laravel Queue Scheduling
What Is Method Chaining In PHP?
Upload File From Frontend Server {GuzzleHttp} To REST API Server In PHP {Laravel}