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

问题描述 投票:1回答:1

我想显示密码和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.And和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>

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

html5 laravel laravel-5.6
1个回答
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以获取更多参考。

让我知道它的工作与否。

© www.soinside.com 2019 - 2024. All rights reserved.