11

try to create validator manually in Lumen. The official documentation is written:

<?php

namespace App\Http\Controllers;
use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PostController extends Controller
{
     /**
     * Store a new blog post.
     *
     * @param  Request  $request
     * @return Response
     */
     public function store(Request $request)
     {
        $validator = Validator::make($request->all(), [
            'title' => 'required|unique:posts|max:255',
            'body' => 'required',
        ]);

        if ($validator->fails()) {
            return redirect('post/create')
                        ->withErrors($validator)
                        ->withInput();
        }

        // Store the blog post...
     }
}

I wrote

<?php

namespace App\Http\Controllers;
use Laravel\Lumen\Routing\Controller as BaseController,
    Validator;

class Welcome extends BaseController
{
    public function index()
    {
        $validator = Validator::make(
            ['test' =>'TestValidation'],
            ['test' => 'required|unique:posts|max:255']
        );
    }
}

but Lumen returns fatal error: Fatal error: Class 'Validator' not found in ...

I have tried to do like in Laravel 5:

use Illuminate\Support\Facades\Validator;

but then Lumen returns Fatal error: Call to a member function make() on a non-object in

Somebody knows how to use the Validator class in Lumen? Thank you.

epod
  • 143
  • 2
  • 6

2 Answers2

19

Validator is a facade. Facades aren't enabled by default in lumen.

If you would like to use the a facade, you should uncomment the

$app->withFacades();

call in your bootstrap/app.php file.

baao
  • 71,625
  • 17
  • 143
  • 203
1

This is for Lumen version 5.3 (as shown in the docs):

use Illuminate\Http\Request;

$app->post('/user', function (Request $request) {
    $this->validate($request, [
    'name' => 'required',
    'email' => 'required|email|unique:users'
 ]);

    // Store User...
});

https://lumen.laravel.com/docs/5.3/validation

Arnold Balliu
  • 1,099
  • 10
  • 21