Laravel 5验证错误未显示 [英] Laravel 5 validation errors are not showing
本文介绍了Laravel 5验证错误未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在学习Laravel 5
,并且已经为用户登录编写了以下代码.现在,我尝试使用以下代码显示验证错误消息:
I am learning Laravel 5
and I have wrote following code for user login. Now I am trying display validation errors messages with this code:
@if(count($errors))
<div class="alert alert-danger">
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
,但未显示任何内容. :(
but nothing is displaying. :(
不显示错误原因的任何提示,如果用户未输入任何值并点击提交按钮.
Any idea whyerrors are not displaying, If user not enter any value and hit submit button.
谢谢.
Route::get('/', function () {
return view('welcome');
});
Route::group(['middleware' => ['web']], function () {
Route::get('/login', ['as' => 'login', 'uses' => 'AuthController@login']);
Route::post('/handleLogin', ['as' => 'handleLogin', 'uses' => 'AuthController@handleLogin']);
Route::get('/home', ['middleware' => 'auth', 'as' => 'home', 'uses' => 'UserController@home']);
Route::get('/logout', ['as' => 'logout', 'uses' => 'AuthController@logout']);
Route::resource('user', 'UserController', ['only' => ['create', 'store']]);
});
login.blade.php
@extends('layouts.master')
@section('content')
<div class="row">
<div class="col-md-6">
<h2>Log in</h2>
<p>Hi, here you can login to your account.</p>
<br>
@if(count($errors))
<div class="alert alert-danger">
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
{!! Form::open(array('route' => 'handleLogin')) !!}
<div class="form-group">
{!! Form::label('email', 'E-Mail Address') !!}
{!! Form::text('email', null, array('class' => 'form-control','placeholder' => 'example@gmail.com')) !!}
</div>
<div class="form-group">
{!! Form::label('password', 'Password') !!}
{!! Form::password('password', array('class' => 'form-control')) !!}
</div>
<div>
{!! Form::token() !!}
{!! Form::submit('Sign In' , array('class' => 'btn btn-primary')) !!}
</div>
{!! Form::close() !!}
<br>
</div>
</div>
@endsection
AuthController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
class AuthController extends Controller
{
public function login()
{
return view('auth.login');
}
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();
}
public function logout()
{
\Auth::logout();
return redirect()->route('login');
}
}
User.php
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public static $login_validation_rules = [
'email' => 'required|email|exists:users',
'password' => 'required'
];
}
推荐答案
我认为您可以修改handleLogin方法以将错误发送到视图,如下所示:
I think you can modify your handleLogin method to send the errors to the view, something like this:
public function handleLogin(Request $request)
{
$validator=$this->validate($request, User::$login_validation_rules);
$data = $request->only('email', 'password');
if(\Auth::attempt($data)) {
return redirect()->intended('home');
}
return back()->withInput()->withErrors($validator->errors());
}
应该可以
您可以阅读Laravel文档来查看解决方案: https://laravel.com/docs/5.0/validation
You can review the solution reading the Laravel Documentation: https://laravel.com/docs/5.0/validation
致谢!
这篇关于Laravel 5验证错误未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文