Using the Illuminate\Validation\Validator class outside Laravel

Jeff
2 min readApr 2, 2018

--

Photo by Daniel Cheung on Unsplash

Laravel’s validation component its very handy especially when you need to validate forms on your PHP projects.
Building your validator could be time-consuming when you are not working with laravel.
Let’s see how to use the Validator component outside Laravel.

Install the dependencies using composer

Add the following to your composer.json file:

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

Then from your console, run:

$ composer install

Creating a new Translator instance

In Laravel all this process happens behind the scenes, so you don’t need to do any extra configuration, but, in this case, we need to follow a couple of steps before to provide all the arguments that the Illuminate\Valitation\Factory::class needs to work.

This is the tricky part, but it’s fairly easy once you have check the source code of each class and played a little with the implementation.

// Create a Filesystem instance
$filesystem = new Illuminate\Filesystem\Filesystem();
// Create a new FileLoader instance specifying the translation path
$loader = new Illuminate\Translation\FileLoader($filesystem, dirname(dirname(__FILE__)) . ‘/lang’);
// Specify the translation namespace
$loader->addNamespace(‘lang’, dirname(dirname(__FILE__)) . ‘/lang’);
// This is used to create the path to your validation.php file
$loader->load($lang = ‘en’, $group = ‘validation’, $namespace = ‘lang’);

$factory = new Illuminate\Translation\Translator($loader, ‘en’);
$validator new Illuminate\Validation\Factory($factory);

You can move this to it’s own class:

<?phpuse 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
);
}
}

You need to have the following tree on your root directory:

/lang/en/validation.php

That file should return an array with the error messages to show when the validation fails:

Then you can use:

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

You can learn more about the validation component by reading the official documentation.

Making it easier

I’ve created a package that you can install using composer ready to use on your PHP projects:

--

--

Jeff

Web developer. Always learning... #fullstack #less #sass #php #laravel #javascript #VueJs