首页 > 解决方案 > Laravel 5.7 验证错误未显示

问题描述

我在下面有以下功能,可以在两个选项中正常登录并重定向,但是我没有收到任何错误!- 我怎样才能覆盖错误消息?

刀:

   <div>
      {{ Form::label('username', 'Username') }}
      {{ Form::text('username', Input::old('username'), array('class' => 'form-control', 'required' => 'required')) }}

       <div class="invalid-feedback">{{ $errors->first('username') }}</div>

功能:

public function processLogin() {

    // Lets create some simple validation rules for login

    $rules = array(

        'username' => 'required',
        'password' => 'required',
    );

    // Lets check the guest filled in all the correct details :)

    $validator = Validator::make(Input::all(), $rules);

    // Lets redirect back to login page if they have not

    if ($validator->fails()) {
        return Redirect::to('/')
            ->withErrors($validator)
            ->withInput(Input::except('password')); //Sends back only room name
    } else {
        // We will create an array of login information :)

        $loginData = array(
            'username' => Input::get('username'),
            'password' => Input::get('password'),
        );

        // Lets Login

        if (Auth::attempt($loginData)) {
            // If they logged in correct lets give them this :)

            return Redirect::away('https://google.co.nz/');
        } else {
            // If not they can have this

            return Redirect::to('/');
        }
    }

标签: phplaravel

解决方案


好的,所以您正在尝试更改“默认”错误消息,而不是创建新规则。我看到上面的一些人一直在回答,但这是我的 2 美分,非常干净和简单。

public function store(Request $request)
{
    $messages = [
        'username.required' => 'You must have a username!',
        'password.required' => 'Please add a password...'
    ];

    $request->validate([
        'username' => 'required',
        'password' => 'required'
    ], $messages);

   // And here is the code that should be executed if the validation is valid

   return 'Everything seems to work!';
}

推荐阅读