As the official documentation doesn’t specify how exactly to create a custom
validation class.

1. Create custom validator class

You can create in any where as you like. For my example, I will create in
app/Libraries/CustomValidator.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php namespace App\Libraries;
class CustomValidator
{
/**
* custom_rule:$param1,$param2,...
*
* @param mixed $attribute
* @param mixed $value
* @param mixed $parameters
* @return bool
*/
public function validateYourCustomRule($attribute, $value, $parameters)
{
// validate
return false;
}
}

2. Update to provider

Edit the file app/Providers/AppServiceProvider.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
\Validator::extend('custom_rule', 'App\Libraries\CustomValidator@validateYourCustomRule');
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}

3. Add the error message to language file

Edit resources/lang/en/validation.php and add this

1
'custom_rule' => 'The :attribute has custom error.',

Test it

1
2
3
4
5
<?php
...
$rules = [
'attr' => 'custom_rule:param1,param2',
];

References: