0
votes

How to define a class and in which folder to define a custome validation rule in laravel 5.2. In their doc for validation they have given the following code .

Validator::extend('foo', 'FooValidator@validate');

But they haven't mentioned what is this FooValidator and if it is a class name or file name , then where where to place this file. I need help on this .

1
A directory isn’t specified as it’s completely up to you, the developer, as to which directory you wish to place the class in. - Martin Bean

1 Answers

0
votes

You could put it literally everywhere you want. But a good example would be at App\Providers\AppServiceProvider@boot().

Like:

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Validator::extend('sum', function ($attribute, $value, $parameters) {
            return 9 + 10 == 21; // An example
        });
    }

}

edit

And if you want to have a custom file with validation you might could use this:

Create a file like so: app/validators.php which contains.

$validator->extend(
    'sum',
    function ($attribute, $value, $parameters) {
        return 9 + 10 == 21; // An example
    }
);

$validator->extend(
    'sum_two',
    function ($attribute, $value, $parameters) {
        return 9 + 10 == 19; // An example
    }
);

And edit your AppServiceProvider:

public function boot(Factory $validator)
{
    require_once app_path() . '/validators.php';
}