How to (Easily) Create a Custom Validation Rule

For some reason, only now I’ve found out about a feature that was introduced in Laravel 5.5 – artisan command that makes your validation rule, similar to Request classes. Let’s see it in action.

Let’s take an example of a form to fill in Summer Olympic Games events – so year and city:

laravel form

Now, let’s create a validation rule that you can enter only the year of Olympic Games:

  • Games started in 1896
  • Year can’t be bigger than current year
  • Number should be divided by 4

Let’s run a command:

php artisan make:rule OlympicYear

Laravel generates a file app/Rules/OlympicYear.php:

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class OlympicYear implements Rule
{

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        //
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'The validation error message.';
    }
}

As I said, it’s similar to Requests classes for validation. We fill in the methods. passes() should return true/false depending on $value condition, which is this in our case:

public function passes($attribute, $value)
{
    return $value >= 1896 && $value <= date('Y') && $value % 4 == 0;
}

Next, we fill in the error message to be this:

public function message()
{
    return ':attribute should be a year of Olympic Games';
}

Finally, how we use this class? In controller's store() method we have this code:

public function store(Request $request)
{
    $this->validate($request, ['year' => new OlympicYear]);
}

Pay attention to syntax, second parameter should be an array and then we create a new object from our Rule class.
That's it!

Laravel validation custom

Like our articles?
Check out our Laravel online courses!

14 COMMENTS

    • You can use it in Request by following :
      1) while create condition in Rule file you have to return true or false as per condition ,like:
      if( $value > 2){
      return false;
      }
      2) After create Rule, you have to include this in Request Like:
      use App\Rules\OlympicYear;

      3) use rule like that in Request
      public function rules()
      {
      return [
      “field_name” => [‘required’, new OlympicYear()],
      ];
      }

  1. I think you got a code error in $this->validate($request, [‘year’ => new OddNumber]);
    should be $this->validate($request, [‘year’ => new OlympicYear]);

    Cheers

  2. This is really the best answer that I found on the net. It is well explained and follows Laravel convention.

    Thank you. Just keep writing tutorials.

  3. We can use also custom validation with Laravel existing validation rules?
    like I have create new custom validation called “IsProductExits” and using that I can also use like this
    ‘product_name’ => [ ‘bail’, ‘required’, new IsProductExits( $request->brand_id, $request->id ), ‘regex:/[A-Za-z0-9]/’, ‘min:1’, ‘max:30’ ],

LEAVE A REPLY

Please enter your comment!
Please enter your name here