0

My login form in blade validates but wont display errors, it returns to login page with blank entries upon failure, how do I go about this?

Initially my route was defined in api.php and was getting same issue so I changed it to web.php and still same. Also value="{{old('name')}}" too dose not work.

Here is the form I wrote in the blade

<h5>Login Your Account</h5> <!-- FORM --> @if ($errors->any()) <div class="alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <form method="POST" action="/login" > {{ csrf_field() }} <div class="form-group"> <label for="name">Name</label> <input type="text" id="name" class="form-control" name="name" placeholder="" value="{{old('name')}}"> @if ($errors->has('name')) <p class="help-block">{{ $errors->first('name') }}</p> @endif </div> <div class="form-group "> <label for="password">Password</label> <input type="password" id="password" class="form-control" name="password" value="{{old('password')}}"> @if ($errors->has('password')) <p class="help-block">{{ $errors->first('password') }}</p> @endif </div> <li class="col-sm-12 text-left"> <button type="submit" class="btn-round">Login</button> </li> </form> 

Here is my controller login function

public function login(Request $request){ $validator= Validator::make($request->all(), ['name'=> "bail|required|max:50|exists:users", 'password' => 'required|min:6|exists:users,password' ] ); if ($validator->fails()){ return redirect()->back()->withInput()->withError($validator); } else{ if (Auth::attempt(['name'=> $request->name, 'password'=>$request->password])){ return redirect('/home'); } } 

Here is my web.php route

Route::get('/', function () { return view('home'); }); Route::post('/login','API\userController@login'); 

And my kernel.php

<?php namespace App\Http; use Illuminate\Foundation\Http\Kernel as HttpKernel; class Kernel extends HttpKernel { /** * The application's global HTTP middleware stack. * * These middleware are run during every request to your application. * * @var array */ protected $middleware = [ \App\Http\Middleware\CheckForMaintenanceMode::class, \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, \App\Http\Middleware\TrimStrings::class, \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, \App\Http\Middleware\TrustProxies::class, ]; /** * The application's route middleware groups. * * @var array */ protected $middlewareGroups = [ 'web' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, // \Illuminate\Session\Middleware\AuthenticateSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, \Laravel\Passport\Http\Middleware\CreateFreshApiToken::class, ], 'api' => [ 'throttle:60,1', 'bindings', ], ]; /** * The application's route middleware. * * These middleware may be assigned to groups or used individually. * * @var array */ protected $routeMiddleware = [ 'auth' => \App\Http\Middleware\Authenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, 'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class, 'can' => \Illuminate\Auth\Middleware\Authorize::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, ]; /** * The priority-sorted list of middleware. * * This forces non-global middleware to always be in the given order. * * @var array */ protected $middlewarePriority = [ \Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\Authenticate::class, \Illuminate\Session\Middleware\AuthenticateSession::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, \Illuminate\Auth\Middleware\Authorize::class, ]; } 

If validation is successful, It redirects to the route I specified, but once it fails it directs back to the log in page with no error displayed

1 Answer 1

2

Laravel's RedirectResponse class has method withErrors() to send validator message bag where as you are calling withError().

See this from Laravel's RedirectResponse.php

/** * Flash a container of errors to the session. * * @param \Illuminate\Contracts\Support\MessageProvider|array|string $provider * @param string $key * @return $this */ public function withErrors($provider, $key = 'default') { $value = $this->parseErrors($provider); $this->session->flash( 'errors', $this->session->get('errors', new ViewErrorBag)->put($key, $value) ); return $this; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.