georgenis's avatar

Validation with Lumen

Hello guys,

maybe i am stupid, but I try to add in my view {{ var_dump($errors) }} and I get following error:

  • Undefined variable: errors

I thought this variable is in every view available? Am I wrong?

My second question is about validation as a method. I can use inside of my controller "$this->validate($request, $rules, $messages), but if the validation fails it redirects me to my "/" address not to my "back()" address.

UPDATE:

I implemented it that way:

$messages = [ 'required' => 'Das Feld :attribute muss befüllt sein', 'same' => 'Die Passwörter sind nicht identisch', 'unique' => 'Der Benutzer existiert bereits' ];

    $rules = [
        'email' => 'required|unique:UserAuthentications|max:255',
        'password' => 'required|same:passwordAgain|max:255'
    ];

    $validator = Validator::make($request, $rules, $messages);
    
    if($validator->fails()) {
        return redirect()
                ->back()
                ->withErrors($validator->errors())
                ->withInput();
    }    

But then I got following error: "Argument 1 passed to Illuminate\Validation\Factory::make() must be of the type array, object given" - What the hell? I gave arrays?

Maybe you can help me :-)

0 likes
2 replies
patkan's avatar

Try something like this, I had the same problem..

use Validator;

$validator = Validator::make($request->all(), [ 'name' => 'required', 'email' => 'required', ]);

    if ($validator->fails()) {
        return redirect($request->path()) // or some path
            ->withErrors($validator)
            ->withInput();
    }

redirect->back() will also work when use Validator

patkan's avatar

Ok I find the solution.

Everything with return back and etc. works great, my problem was with the session configuration. I'm running a Lumen api on shared hosting with no Memcached installed (only Memcache) so when I set SESSION_DRIVER in .env file to be equal to cookie everything now is working :).

In .env: CACHE_DRIVER=cookie SESSION_DRIVER=cookie QUEUE_DRIVER=database

And in my controller (for example) just: $this->validate($request, [ 'name' => 'required', 'email' => 'required|email', ]);

Please or to participate in this conversation.