Ad

"Unresolvable Dependency Resolving" Error Custom Validation In Laravel 5.1

- 1 answer

To make a custom validation rule in laravel 5.1 , I created a class named CustomValidators in App/Validatiors directory like this :

namespace  App\Validators;

    use App\Classes\Utilities;

    class CustomValidators extends \Illuminate\Validation\Validator
    {
        public function DateTime ($field , $value ,$param, $validator){
            dd($value);
        }
    }

And in AppServiceProvider.php , I added this :

Validator::extend('date_time', 'App\Validators\[email protected]');

and in store() function of a Controller I used it like this :

$rules = array (
                'start_publish' => 'date_time',
            );

But when running App , this error occured :

BindingResolutionException in Container.php line 824:
Unresolvable dependency resolving [Parameter #1 [ <required> array $data ]] in class Illuminate\Validation\Validator

Of curse when I define DateTime() function directly in AppServiceProvider.php all things works like this :

Validator::extend('date_time', function ($field , $value ,$param, $validator){
                dd($value);
            });

What is Problem while I follow custom validation laravel docs

Ad

Answer

I found the problem

I should not to extend \Illuminate\Validation\Validator when defining CustomValidators class beacause it automatically extends Validator class via :

Validator::extend('date_time', 'App\Validators\[email protected]');

in fact CustomValidators class must be like this :

namespace  App\Validators;

    use App\Classes\Utilities;

    class CustomValidators
    {
        public function DateTime ($field , $value ,$param, $validator){
            dd($value);
        }
    }
Ad
source: stackoverflow.com
Ad