A
A
Alexander Sharomet2016-08-25 21:28:58
Laravel
Alexander Sharomet, 2016-08-25 21:28:58

Why doesn't Validator $errors work in Laravel 5.2?

Hello.
Faced such a problem. The validator is not working. It simply does not display anything, redirects back and that's it. What could be the problem?
user.php

namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable{
    protected $fillable = [
        'name',  'email', 'password',
    ];
    protected $hidden = [
        'password', 'remember_token',
    ];
    public static $login_validation_rules = [
        'email' => 'required|email|exists:users',
        'password' => 'required'
    ];
}

AuthController.php
public function handleLogin(Request $request){
        $this->validate($request, User::$login_validation_rules);
    	$data = $request->only('email', 'password');
    	if(\Auth::attempt($data)){
    		return redirect()->intended('home');
    	}
    	return back()->withInput()->withErrors(['Email' => 'Email или пароль не верны']);
    }

@if(count($errors))
    <div class="alert alert-error">
      <ul>
        @foreach($errors->all() as $error)
          <li>{{ $error }}</li>
        @endforeach
      </ul>
    </div>
  @endif

There are no errors, just nothing happens.
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Kolesnikov, 2016-08-26
@sharomet

Why write these rules in the model
If you don’t want to write them in the controller, then you need to write them in the Form Requests
form-request-validation
The laravel standard authorization already has a validation method in the AuthController, as I understand it, you don’t use it

A
Alexander Sharomet, 2016-08-26
@sharomet

I'm writing a bike because I don't fully understand how Laravel works.
Answer to
kernel.php question

protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Session\Middleware\StartSession::class,
    ];

Why write these rules in the model
If you don’t want to write them in the controller, then you need to write them in Form Requests

Thanks ... I missed it ..

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question