Laravel 5 - where to place reusable custom validator?

I am wondering where is the place for the custom validator that I want to use in more than one place?

For example, I have a min_image_sizevalidator:

Validator::extend('min_image_size', function($attribute, $value, $parameters) {
    $imageSize = getimagesize($value->getPathname());
    return ($imageSize[0] >= $parameters[0] && $imageSize[1] >= $parameters[1]);
});

Where should I put it on the right Laravel path?

+4
source share
2 answers

Definitely extend the validator to a service provider . You can use an existing one app/Providers/AppServiceProvider.phpor create another one for verification.

Then in the method boot()add the following:

public function boot(){
    $this->app['validator']->extend('min_image_size', function($attribute, $value, $parameters) {
        $imageSize = getimagesize($value->getPathname());
        return ($imageSize[0] >= $parameters[0] && $imageSize[1] >= $parameters[1]);
    });
}

:

$this->app['validator']->extend('min_image_size', 'MyCustomValidator@validateMinImageSize');
+5

, , .

-2

All Articles