为什么不在Laravel 5.6中显示验证错误消息

时间:2019-03-18 07:15:47

标签: html5 laravel laravel-5.6

我想显示密码错误和Confirm_password匹配的错误消息。下面是我的代码,如果密码不匹配,请帮助我如何显示错误消息。

PasswordResetController.php

  public function reset(Request $request)
      {

        $request->validate([
            'email' => 'required|string|email',
            'password' => 'required|string|min:6|max:12|confirmed',
            //'token' => 'required|string'
        ]);
        $passwordReset = PasswordReset::where([
           // ['token', $request->token],
            ['email', $request->email]
        ])->first();
        if (!$passwordReset)
            return response()->json([
                'message' => 'This password reset token is invalid.'
            ], 404);
        $user = User::where('email', $passwordReset->email)->first();
        if (!$user)
            return [
                'message' => 'We can not find a user with that e-mail address.'
            ];
        $user->password = bcrypt($request->password);
        \Session::flash('flash_message',' Password reset successfully!.');
        $user->save();

        $user->notify(new PasswordResetSuccess($passwordReset));
        return view('changePassword', compact('user'));
    }

这是我的控制器函数,其中定义了laravel的方法。changePassword.blade.php是我的视图文件。如何在laravel中显示验证错误消息。

changePassword.blade.php

    <!DOCTYPE html>
        <html lang="en">
        <head>

          <meta charset="utf-8">
          <meta name="viewport" content="width=device-width, initial-scale=1">
          <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
          <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
        </head>
        <body style="background-image:url({{url('files/bk1.jpg')}})">

        <div class="container">
          <h2 style="color: white;">Change Password</h2>
          <form method="post" class="form-horizontal" action="{{ route('reset') }}">
            @csrf
           @if(Session::has('flash_message'))
            <div class="alert alert-success"><span class="glyphicon glyphicon-ok"></span><em> {!! session('flash_message') !!}</em></div>
            @endif
            @if(Session::has('message'))
              <p class="alert {{ Session::get('alert-class', 'alert-info') }}">{{ Session::get('message') }}</p>
            @endif

            <div class="form-group">
              <label style="color: white" class="control-label col-sm-2" for="email">Email:</label>
              <div class="col-sm-4">
                <input type="email" class="form-control" id="email" placeholder="Enter email" name="email">
              </div>
            </div>
            <div class="form-group">
              <label style="color: white" class="control-label col-sm-2" for="pwd">Password:</label>
              <div class="col-sm-4">          
                <input type="password" class="form-control" id="pwd" placeholder="Enter password" name="password">
              </div>
            </div>
             <div class="form-group">
              <label  style="color: white" class="control-label col-sm-2" for="pwd">Confirm Password:</label>
              <div class="col-sm-4">          
                <input type="password" class="form-control" id="pwd" placeholder="Enter Confirm Password" name="password_confirmation">
              </div>
            </div>

            <div class="form-group">        
              <div class="col-sm-offset-2 col-sm-10">
                <button type="submit" class="btn btn-primary">Submit</button>
              </div>
            </div>
          </form>
        </div>

        </body>
        </html>

如果用户密码并确认密码不匹配,我想显示错误消息

1 个答案:

答案 0 :(得分:1)

在密码输入字段后添加以下代码:

@if ($errors->has('password'))
    <span class="invalid-feedback" role="alert">
        <strong>{{ $errors->first('password') }}</strong>
    </span>
@endif

因此它看起来像:

<div class="form-group">
    <label style="color: white" class="control-label col-sm-2" for="pwd">Password:</label>
    <div class="col-sm-4">
        <input type="password" class="form-control" id="pwd" placeholder="Enter password" name="password">

        @if ($errors->has('password'))
            <span class="invalid-feedback" role="alert">
                <strong>{{ $errors->first('password') }}</strong>
            </span>
        @endif
    </div>
</div>
  

您可以检查https://laravel.com/docs/5.6/validation以获得更多参考。

让我知道它的工作与否。