Laravel 5验证错误未显示

时间:2016-04-08 21:09:10

标签: php laravel laravel-5

我正在学习import random yes = ['yes', 'Yes', 'Yeah', 'yeah', 'Yea', 'yea', 'Mhmm', 'mhmm', 'Mhm', 'mhm'] no = ['No', 'no', 'Nah', 'nah'] good = ['Good', 'good', 'Great', 'great', 'Okay', 'okay', 'Ok', 'ok', 'OK'] bad = ['Bad', 'bad', 'Not good', 'not good', 'Not well', 'not well'] random_yes = random.choice(yes) random_no = random.choice(no) random_good = random.choice(good) random_bad = random.choice(bad) greetings = ['Hola', 'Hello', 'Hi', 'Hey!','hey...'] random_greeting = random.choice(greetings) print(random_greeting) name = input('What is your name?') print('Hello', name,) question1 = ['How are you?','How are you doing?','How is your day?','How is your day going?'] random_question1 = random.choice(question1) ur1 = input(random_question1) if good in ur1: print("That's good") elif bad in ur1: print("I'm sorry") else: print("I don\'t understand") ,我已经为用户登录编写了以下代码。现在我正在尝试使用以下代码显示验证错误消息:

Laravel 5

但没有显示任何内容。 :(

任何想法都没有显示错误,如果用户没有输入任何值并按下提交按钮。

感谢。

routes.php文件

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

login.blade.php

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']]);
});

AuthController.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

user.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');
    }
}

4 个答案:

答案 0 :(得分:2)

我认为您可以修改handleLogin方法以将错误发送到视图,如下所示:

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

问候!

答案 1 :(得分:0)

为什么不使用内置寄存器功能? 如果您还想要,您也可以使用Validator https://laravel.com/docs/master/validation

编写自己的注册表
$validator = \Validator::make($request->all(), User::$login_validation_rules);
if($validator->passes()) {
   // authenticate...
   // If auth failed, add message to validator
   $validator->getMessageBag()->add('password', 'Email or password invalid');   
}
return redirect()->back()->withErrors($validator)->withInput();

问题是您只处理输入错误,如果用户输入无效的电子邮件或密码,则不会显示。

答案 2 :(得分:0)

laravel 5.2。*

/* app/Http/Kernel.php */

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel {

/**
 * The application's route middleware groups.
 *
 * @var array
 */
protected $middlewareGroups = [
    'web' => [
      /*  you need these two Middleware  */
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    ]



}

答案 3 :(得分:0)

我正在为那些可能遇到同样问题/解决方案的人添加此功能。我是Laravel(5.2)的新手,并且一直在尝试授权和路由的不同方面。有一次,我的LaravelCollective HTML错误包开始迷路了。经过多次挫折后我发现了这个 -

我为需要授权的网页创建了一个路由组。我在该组中放置了一个表单页面。我忘记做的是从控制器中删除对该页面的授权中间件的调用 - $ this-&gt; middleware('auth');这导致请求两次调用授权。它没有强制我登录两次,但它确实导致重新创建会话并且所有以前的保存数据都丢失了。运行页面几次后,我会得到许多会话文件。