epod
epod

Reputation: 143

Class 'Validator' not found in Lumen

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.

Upvotes: 11

Views: 7893

Answers (2)

Arnold Balliu
Arnold Balliu

Reputation: 1119

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

Upvotes: 1

baao
baao

Reputation: 73301

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.

Upvotes: 19

Related Questions