简体   繁体   中英

Illuminate Validator in stand-alone non-Laravel application

I'm building an API using Slim and the Illuminate Database package with Eloquent models etc. I have instantiated the database handler using Capsule as shown in the README . However, now I want to use the validation features on my models without installing the full Laravel suite, but I cannot quite wrap my head around the design of this library.

How would I go about this? It seems like the documentation provided for Laravel is pretty much expecting that you use Laravel out of the box.

Here is a solution for the current version: Laravel 5.4. The composer.json file:

{ "name": "Validation standalone", "require": { "php": ">=5.6.4", "illuminate/validation": "5.4.*", "illuminate/translation": "5.4.*" } }

Note that we must also require "illuminate/translation": "5.4.*". And then in your php file:

use Illuminate\Validation;
use Illuminate\Filesystem;
use Illuminate\Translation;

include 'vendor/autoload.php';

$filesystem = new Filesystem\Filesystem();
$fileLoader = new Translation\FileLoader($filesystem, '');
$translator = new Translation\Translator($fileLoader, 'en_US');
$factory = new Validation\Factory($translator);

$messages = [
    'required' => 'The :attribute field is required.',
];

$dataToValidate = ['title' => 'Some title'];
$rules = [
    'title' => 'required',
    'body' => 'required'
];

$validator = $factory->make($dataToValidate, $rules, $messages);

if($validator->fails()){
    $errors = $validator->errors();
    foreach($errors->all() as $message){
        var_dump($message);
    }
}

Here I have intentionally missed the "body" field in the data provided for validation, so that a validation error is displayed.

As for early 2021 the solution from @vivanov here works perfectly with Laravel 8 packages.

Here I add the ability to use default Laravel validation messages as using your own is too annoying.

Here is what you have to change in the @vinvanov solution.

  1. Upddate composer.json
    "require": {
        "illuminate/validation": "^8.25",
        "illuminate/translation": "^8.25"
    },
  1. Copy Laravel validation messages file to your project/laravel/en/validation folder.

  2. Amend @ivanov's solution code as follows (only changes are reflected)

$translationDir = dirname(__DIR__, 4) . '/project/laravel/en/validation';

$fileLoader = new Translation\FileLoader($filesystem, $translationDir);
$fileLoader->addNamespace('lang', $translationDir);
$fileLoader->load('en', 'validation', 'lang');

$validator = $factory->make($dataToValidate, $rules);

See the full code with more comments in my gist ;

This is it. You have the default Laravel messages in place working for you.

Here is the Laravel validation docs .

PS: Credits also go to this blog post from Jeff.

PPS: This is brilliant that despite version jump from 5.6 to 8.25 the packages interface is stable and works seamlessly. So mature and insightful open source attitude and great care about developers from Taylor Otwell. You can only apreciate the utmost convenience of this when you work outside of modern PHP frameworks within a shitty PHP codebase.

I hate to suggest this but the Laravel validator is probably not what you want. I suggest having a look at the validator classes in either Symfony or Zend Framework (2+). They work quite well as stand-alone validators and in fact I am using the ZF2 form classes and validator in a Laravel project at the moment because the Laravel form and validator classes are just not up to scratch.

It's probably not the answer you wanted to hear but it might save you some pain in the long term.

I was just wondering the same thing, and here I am a year later finding delatbabel's answer to be seriously wanting. I did find the following Gist where spekkionu has a fairly simple setup to get you started. (It's working on my machine?? ;P ) It shows how to make the translator for the factory, etc, etc. It's all included when you import illuminate/validation with composer.

Hope it helps: https://gist.github.com/spekkionu/e9103993138e666f9f63

Best,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM