Custom Validation Rules In PHP Laravel (Using Artisan Command)


Share On        


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.


Prerequisites


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.


Step 1 - Create Laravel Validation Rule


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



Step 2 - Know Your Validation Rule Class Details


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()
    {
        
    }
}


__construct Function


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 
  }
}


Passes Function


By default Laravel passes form attribute ($attribute) and its value ($value) in passes method as seen in the code.


public function passes($attribute, $value)


Message Function


public function message()


When the validation fails you can pass the custom messages from this function.



Step 3 - GoogleRecaptcha Rule Class After Adding Code


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.';
    }
}


Step 4 - Using In ContactsController Store Method


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 */
}



Conclusion


You learnt how to do added custom validation rules. So go ahead and implement them in your projects.




AUTHOR

Channaveer Hakari

I am a full-stack developer working at WifiDabba India Pvt Ltd. I started this blog so that I can share my knowledge and enhance my skills with constant learning.

Never stop learning. If you stop learning, you stop growing