首页 > Laravel 5.2 用户认证,怎样用Flash Message显示错误信息?

Laravel 5.2 用户认证,怎样用Flash Message显示错误信息?

默认的提示是这样的:

怎样改成用$request->session()->flash()来显示这些信息呢?而且只显示第一条。
要求不在默认的位置(也就是图片里面的红字位置)显示出错信息。


我想知道你的变量如何替换成中文的


多次参考Laravel中文文档后自己解决了。
在AuthController里面手动验证。
登录函数:

use Auth;
use Validator;
use Illuminate\Http\Request;

public function postLogin(Request $request) {
    $validator = Validator::make($request->all(), [
        'username' => 'bail|required|min:5|max:30|unique:users',
        'password' => 'bail|required|min:8|max:50',
    ]);
    
    if ($validator->fails()) {
        $errors = $validator->errors()->all();
        if (count($errors) > 0) {
            Flash(implode('<br>', $errors), 'error');  //我使用了laracasts/flash这个扩展包,如果你没安装,用$request->session->flash()也是一样的
        }

        return redirect('/login')
                   ->withInput();  //不使用->withErrors就不会显示红字
    }

    //验证登录代码省略...
}

注册函数类似。

【热门文章】
【热门文章】