尝试在Request类中添加Messagebag:Laravel 5.3

时间:2017-01-30 23:46:58

标签: php laravel laravel-5.3

原始请求类

class LoginRequest extends Request
{

    public function wantsJson() {
        return true;
    }

    public function authorize() {
        return true;
    }

    public function rules() {
        return [
            'EmailAddress'  => 'required',
            'Password'      => 'required',
        ];
    }

    public function messages() {
        return [
            "EmailAddress.required" => trans("login.RequiredEmailAddress"),
            "Password.required"     => trans("login.RequiredPassword")
        ];
    }

}

修改后的请求类

class LoginRequest extends Request
{

    public function wantsJson() {
        return true;
    }

    public function authorize() {
        return true;
    }

    public function rules() {
        return [
            'EmailAddress'  => 'required',
            'Password'      => 'required',
        ];
    }

    public function messages() {
        return [
            "EmailAddress.required" => trans("login.RequiredEmailAddress"),
            "Password.required"     => trans("login.RequiredPassword")
        ];
    }

    public function formatErrors(\Illuminate\Contracts\Validation\Validator $validator) {
        if($validator->fails()) {
            $validator->errors()->add('AttemptsLeft', '4 attempts left');
        }
        parent::formatErrors($validator);
    }
}

在修改后的代码中,我正在尝试添加一个密钥,以告知用户有待登录的总尝试次数。

以下是使用的代码。

if($validator->fails()) {
    $validator->errors()->add('AttemptsLeft', '4 attempts left');
}

我收到以下错误

  

参数1传递给   Illuminate \ Foundation \ Http \ FormRequest :: response()必须是类型   数组,null给定,调用   C:\ XAMPP \ htdocs中\项目\项目\ Laravel \供应商\ laravel \框架的\ src \照亮\基金会\ HTTP \ FormRequest.php   在第117行并定义

您能否建议我如何使其正常工作?

1 个答案:

答案 0 :(得分:1)

您忘了return parent::formatErrors($validator);