Laravel“密码错误”错误消息

时间:2017-08-03 05:59:11

标签: php laravel-5.4

我正在Laravel 5.4中创建登录功能,我想在密码不正确时在视图中显示错误消息。此外,我有一个自定义消息,用于帐户审批,所以这对我来说有点困难。与此同时,我将这些消息放在一起但不是非常用户友好。我该如何分开它们?

这是我的控制者:

public function login(Request $request)
{
    // validate the form data
    $this->validate($request, [
        'email' => 'required|email|exists:users,email',
        'password' => 'required|min:6'
    ]);

    // attempt to log
    if (Auth::attempt(['approve' => '1', 'email' => $request->email, 'password' => $request->password ], $request->remember)) {

        // if successful -> redirect forward
        return redirect()->intended(route('user.overview'));
    }

    // if unsuccessful -> redirect back
    return redirect()->back()->withInput($request->only('email', 'remember'))->withErrors([
        'approve' => 'Wrong password or this account not approved yet.',
    ]);
}

结果我想用两个单独的消息替换Wrong password or this account not approved yet

如果显示密码错误:Password is wrong 如果帐户未获批准,请显示:This account not approved yet

3 个答案:

答案 0 :(得分:4)

您可以为每个验证规则传递自定义错误消息,您可以执行以下操作:

    public function login(Request $request)
    {
        //Error messages
        $messages = [
            "email.required' => "Email is required",
            "email.email' => "Email is not valid",
            "email.exists' => "Email doesn't exists",
            "password.required" => "Password is required",
            "password.min" => "Password must be at least 6 characters"
        ];

        // validate the form data
        $validator = Validator::make($request->all(), [
                'email' => 'required|email|exists:users,email',
                'password' => 'required|min:6'
            ], $messages);

        if ($validator->fails()) {
            return back()->withErrors($validator)->withInput();
        } else {
            // attempt to log
            if (Auth::attempt(['approve' => '1', 'email' => $request->email, 'password' => $request->password ], $request->remember)) {
                // if successful -> redirect forward
                return redirect()->intended(route('user.overview'));
            }

            // if unsuccessful -> redirect back
            return redirect()->back()->withInput($request->only('email', 'remember'))->withErrors([
                'approve' => 'Wrong password or this account not approved yet.',
            ]);
        }
    }

在此之前,您必须包含Validator类:

use Illuminate\Support\Facades\Validator;

答案 1 :(得分:0)

您可以这样使用:

return Redirect::back()->withInput(Input::all());

如果你正在使用Form Request Validation,这正是Laravel将错误和给定输入重定向的方式。

摘自\Illuminate\Foundation\Validation\ValidatesRequests

return redirect()->to($this->getRedirectUrl())
                    ->withInput($request->input())
                    ->withErrors($errors, $this->errorBag());

<强>控制器:

public function login(Request $request)
{
    // validate the form data
    $this->validate($request, [
        'email' => 'required|email|exists:users,email',
        'password' => 'required|min:6'
    ]);

    // attempt to log
    if (Auth::attempt(['approve' => '1', 'email' => $request->email, 'password' => $request->password ], $request->remember)) {

        // if successful -> redirect forward
        return redirect()->intended(route('user.overview'));
    }

    // if unsuccessful -> redirect back
    return Redirect::back()
            ->withInput()
            ->withErrors(
                [
                    'password' => 'Wrong Password',
                ],
                [
                    'approve' => 'Account not approved',
                ],
            );
}

答案 2 :(得分:0)

无需编写新的自定义登录方法,我们就可以通过Auth默认登录过程轻松处理自定义的错误密码消息。

从以下位置打开 LoginController app/Http/Controllers/Auth/

如果未在控制器顶部退出,请包含 Request

use Illuminate\Http\Request;

最后在LoginController的最底部添加下面的代码行,以处理带有自定义消息的响应错误



    /**
     * Get the failed login response instance.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse
     */
    protected function sendFailedLoginResponse(Request $request)
    {
        $errors = [$this->username() => trans('auth.failed')];
    
        // Load user from database
        $user = \App\User::where($this->username(), $request->{$this->username()})->first();
    
        if ($user && !\Hash::check($request->password, $user->password)) {
            $errors = ['password' => 'Wrong password'];
        }
    
        if ($request->expectsJson()) {
            return response()->json($errors, 422);
        }
    
        return redirect()->back()
            ->withInput($request->only($this->username(), 'remember'))
            ->withErrors($errors);
    }