make Laravel 8's Validator class work outside the framework

Viewed 398

So I installed this package https://packagist.org/packages/illuminate/validation into my project with the command composer require illuminate/validation where you get a stand-alone version of Laravels validator. Then when I tried to use it like the docs say you should https://laravel.com/docs/8.x/validation and try to implement it like this

    $validated = Validator::make($data, [
        'token' => 'required',
        'email' => 'required',
        'password' => 'required',
    ])->validate();

I get an 'message' => 'A facade root has not been set.', also this is the $data object

    array (
        'token' => 'c11019348c3876f033603a0d0030ad3f0c9ec7d7ae211a49',
        'email' => 'ewaeaw',
        'password' => 'ewaeaw',
      ),

so do I need to invoke the library somewhere or what am I'm doing wrong cause the Github readme of the package just says https://github.com/illuminate/validation [READ ONLY] Subtree split of the Illuminate Validation component (see laravel/framework) so what steps am I missing so it works fine so that I can implement it into my project

I also have looked into this Fail to make Laravel 4's Validator class work outside the framework it says you can implement it with this line $factory = new \Illuminate\Validation\Factory(new \Symfony\Component\Translation\Translator('en')); but the class \Illuminate\Validation\Factory doesn't exist anymore. cause the namespace is Illuminate\Support\Facades\Validator prob cause it 9 years old? so some one has any up to date method of doing this? also looking at the vendor map there doesn't exist any factory class inside the Facades folder

2 Answers

For me the solution was given in the comments by Beri on the question itself, he posted a link to a method that also works on the most recent versions up to (9,14) of the illuminate/validation class. i will post it here as answer. Because many of the answers i found on google did not work with the newer versions of the illuminate/validation class. So i think it will probably help others out to.

It requires the need of:

"require": {
    "illuminate/filesystem": "^9.14",
    "illuminate/translation": "^9.14",
    "illuminate/validation": "^9.14"
}

This nice class created by jeff on his website:

<?php
use Illuminate\Validation;
use Illuminate\Translation;
use Illuminate\Filesystem\Filesystem;
class ValidatorFactory
{
    private $factory;
    
    public function __construct()
    {
        $this->factory = new Validation\Factory(
            $this->loadTranslator()
        );
    }
    protected function loadTranslator()
    {
        $filesystem = new Filesystem();
        $loader = new Translation\FileLoader(
            $filesystem, dirname(dirname(__FILE__)) . '/lang');
            $loader->addNamespace(
                'lang',
                dirname(dirname(__FILE__)) . '/lang'
            );
        $loader->load('en', 'validation', 'lang');
        return new Translation\Translator($loader, 'en');
    }
    public function __call($method, $args)
    {
        return call_user_func_array(
            [$this->factory, $method],
            $args
        );
    }
}

It needs a translation file that needs to be placed in the project folder lang/en/validation.php Translation for any language can be found on github

If done correctly you can start using validation class like so:

$validator = (new ValidatorFactory())->make(
    $data = ['email'=>'thisfailsasemail'],
    $rules = ['email'=>'required|email']
);
// $validator->fails();
// $validator->passes();
// $validator->errors();

you can use validation in simple way like the following

use Illuminate\Support\Facades\Validator;

        $validation=Validator::make($request->all(),[
        'token'=>'required',
        'email'=>'required',
        'password'=>'required',
    ]);
    if($validation->fails())
    return response()->json($validation->errors());

you can test it from postman or any other way

hopefully that help you

Related