【问题标题】:Why not showing validation error message in laravel 5.6为什么不在 laravel 5.6 中显示验证错误消息
【发布时间】:2019-08-08 12:26:46
【问题描述】:

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

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>

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

【问题讨论】:

  • 你推荐https://laravel.com/docs/5.6/validation了吗?您可以查看名为Displaying The Validation Errors 的部分。

标签: html laravel laravel-5.6


【解决方案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以获取更多参考。

让我知道它的工作与否。

【讨论】:

  • 未定义变量:错误(查看:C:\xampp\htdocs\ionic\api-passport\resources\views\changePassword.blade.php)
  • 这条路线在你的web.php吗?
  • 你的route 必须有web 中间件。
猜你喜欢
  • 1970-01-01
  • 2012-12-30
  • 2019-12-31
  • 2021-12-19
  • 2023-03-23
  • 2020-04-28
  • 2017-01-06
  • 2018-11-05
  • 2017-12-19
相关资源
最近更新 更多