2

I created laravel 5.2 project. I have been created login page but I am stuck on display validation errors message on blade template. I don't know what I'm doing wrong?

Please help me for this issue. Thanks!

View

 <div> <input type="text" name="email" class="form-control" placeholder="Email"/> @if($errors->has('email')) <p>{{ $errors->first('email') }}</p> @endif </div> 

Controller

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\User; use Illuminate\Support\Facades\Validator; use Illuminate\Support\Facades\Input; use Illuminate\Support\Facades\Redirect; use Illuminate\Support\MessageBag; class AuthController extends Controller { public function login() { return view('user.login'); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function handlelogin(Request $request) { $messages = array( 'email.required'=>'You cant leave Email field empty', 'password.required'=>'You cant leave name field empty' ); $rules = array( 'email' => 'required|email|max:20', 'password' => 'required' ); $validator = Validator::make($request->all(),$rules,$messages); if ($validator->fails()) { return redirect()->back()->withErrors($validator->errors()); }else{ $data = $request->only('email','password'); if(\Auth::attempt($data)){ return redirect()->intended('/'); } return back()->withInput(); } } } 

2 Answers 2

3

You don't need to check for errors, just do as following

{!! $errors->first('email', '<div class="error-block">:message</div>') !!} 

please note that "error-block" is a custom css class and you can customize html template according to your choice.

Retrieving An Error Message With A Format


Sign up to request clarification or add additional context in comments.

5 Comments

add {{dd($errors->all())}} to view. then check your error message is in there
Ok, I found this message. object(Illuminate\Support\ViewErrorBag)#185 (1) { ["bags":protected]=> array(0) { } }
First load the page in browser, then add {{dd($errors->all())}}. Then fill the form with invalid data and submit. What's the output?
After submit the form. Only display [ ] .
remove the dd and try with $this->validate($request, $rules); on the controller instead of Validator::make()
0

I have different approach.

Below input field I include additional view:

@include('input-errors', ['inputName' => 'inputName']) #For your case it would be 'email' 

input-errors.blade.php

@foreach ($errors->get($inputName) as $message) <span class="input-error">{{ $message }}</span> @endforeach 

Styles.css

.input-error { color: #ff5555; } 

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.